auto-api-tests-gen-from-swagger
v1.1.6
Published
Generate API test automation setup from Swagger/OpenAPI specifications using Playwright. Configure your Swagger file path and generate tests on demand.
Maintainers
Readme
🚀 auto-api-tests-gen-from-swagger
Automatically generate complete API test automation setup from Swagger/OpenAPI specifications using Playwright.
This npm package automatically creates a fully functional API testing project with comprehensive lifecycle hooks, configuration files, and CI/CD pipeline setup when installed.
✨ Features
- 🎯 Auto-generate Playwright tests from Swagger/OpenAPI specs
- 🔄 Complete lifecycle hooks (beforeAll, afterAll, beforeEach, afterEach)
- 📁 Automatic project setup on installation
- 🛠️ Configuration files generated automatically
- 📊 Custom reporters for detailed test results
- 🎨 Clean test organization with visual separators
- 🔧 Zero configuration - works out of the box
- 📝 Auto-generated documentation
🚀 Quick Start
For New Projects
# 1. Create a new project
mkdir my-api-tests
cd my-api-tests
npm init -y
# 2. Install the package (this sets up the project structure)
npm install auto-api-tests-gen-from-swagger
# ✅ This command will:
# - Set up all project structure
# - Install Playwright and browsers
# - Create configuration files
# 3. If auto-setup doesn't work, run:
npx auto-api-tests-gen setup
# 4. Add your Swagger file
mkdir swagger-json
# Copy your Swagger/OpenAPI JSON file to swagger-json/your-api.json
# 5. Configure your API and Swagger file path
# Update env_values.json with:
# - baseUrl: your API base URL
# - token: your auth token
# - swagger_file_path: "swagger-json/your-api.json"
# 6. Generate tests from your Swagger file
npx auto-api-tests-gen generate-tests
# 7. Run tests
npx playwright test testsFor Existing Projects
# 1. Install in your existing Node.js project
npm install auto-api-tests-gen-from-swagger
# ✅ Project structure and dependencies are set up!
# 2. If auto-setup doesn't work, run:
npx auto-api-tests-gen setup
# 3. Add your Swagger files
mkdir swagger-json
# Copy your Swagger/OpenAPI JSON files to swagger-json/your-api.json
# 4. Configure env_values.json with your API details and Swagger path
# Update:
# - baseUrl: your API URL
# - token: your auth token
# - swagger_file_path: "swagger-json/your-api.json"
# 5. Generate tests from your Swagger file
npx auto-api-tests-gen generate-tests
# 6. Run tests
npx playwright test tests🎯 What Gets Created Automatically
When you install this package, it automatically creates:
your-project/
├── swagger-json/ # 📁 Place your Swagger/OpenAPI files here
│ └── example-petstore.json # Example file provided
├── src/
│ ├── generate-tests.js # 🔧 Test generation engine
│ └── summary-table-reporter.js # 📊 Custom reporter
├── tests/ # 🧪 Generated test files (auto-created)
├── testdata/ # 📋 Generated test data (auto-created)
├── test-results/ # 📈 Test execution results
├── playwright-report/ # 📊 HTML test reports
├── env_values.json # 🔧 Environment configuration
├── playwright.config.js # ⚙️ Playwright configuration
├── azure-pipelines.yml # 🔄 CI/CD pipeline
├── .gitignore # 📝 Git ignore rules
└── README.md # 📖 Project documentation📋 Updated Scripts in package.json
The package automatically adds these scripts to your package.json:
{
"scripts": {
"generate-tests": "node src/generate-tests.js",
"test": "npx playwright test tests",
"test:headed": "npx playwright test tests --headed",
"test:debug": "npx playwright test tests --debug",
"show-report": "playwright show-report",
"install-browsers": "playwright install"
}
}🛠️ Usage Workflow
1. Add Your Swagger Files
# Copy your Swagger/OpenAPI JSON files to the swagger-json folder
cp your-api-spec.json swagger-json/2. Configure Environment
Update env_values.json:
{
"baseUrl": "https://your-api.example.com",
"token": "your-auth-token-here",
"swagger_file_path": "swagger-json/your-api.json"
}3. Generate Tests
npx auto-api-tests-gen generate-tests4. Run Tests
# Run all tests
npx playwright test tests
# Run with visible browser
npx playwright test tests --headed
# Debug mode
npx playwright test tests --debug
# View HTML report
npm run show-report🔄 Test Lifecycle Hooks
Every generated test file includes comprehensive hooks:
// beforeAll - runs once before all tests in the file
test.beforeAll(async ({ request }) => {
// Set up prerequisite data shared across tests
console.log('Setting up prerequisite data...');
});
// afterAll - runs once after all tests in the file
test.afterAll(async ({ request }) => {
// Clean up prerequisite data
console.log('Cleaning up prerequisite data...');
});
// beforeEach - runs before each individual test
test.beforeEach(async ({ request }, testInfo) => {
// Set up test-specific data
console.log(\`Setting up for test: \${testInfo.title}\`);
});
// afterEach - runs after each individual test
test.afterEach(async ({ request }, testInfo) => {
// Clean up test-specific data
console.log(\`Cleaning up after test: \${testInfo.title}\`);
});📊 Generated Test Structure
const { test, expect, request } = require('@playwright/test');
let createdId;
let prerequisiteData = {};
const env = require('../env_values.json');
const baseUrl = env.baseUrl || '';
const authHeaders = { Authorization: 'Bearer ' + (env.token || '') };
// ... lifecycle hooks ...
// ################################################################
// ######################## TEST CASES ##########################
// ################################################################
test('POST_users_201', async ({ request }) => {
const response = await request.post(\`\${baseUrl}/users\`, {
data: testData_post_users_201,
headers: authHeaders
});
expect(response.status()).toBe(201);
});🔧 Configuration Files
env_values.json
{
"baseUrl": "https://api.example.com",
"token": "your-auth-token-here",
"swagger_file_path": "swagger-json/your-api.json"
}- baseUrl: Your API base URL
- token: Authentication token for API requests
- swagger_file_path: Path to your Swagger/OpenAPI JSON file
playwright.config.js
- Configured for API testing
- Includes custom summary reporter
- Set up for multiple browsers
- CI/CD ready configuration
azure-pipelines.yml
- Complete Azure DevOps pipeline
- Includes test generation and execution
- Publishes test results and reports
🔄 CI/CD Integration
The generated azure-pipelines.yml includes:
- Node.js setup
- Dependency installation
- Playwright browser installation
- Test generation from Swagger
- Test execution
- Report publishing
For GitHub Actions
Create .github/workflows/api-tests.yml:
name: API Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npx playwright install --with-deps
- run: npm run generate-tests
- run: npx playwright test tests📈 Advanced Features
Multiple Swagger Files
- Place multiple Swagger files in
swagger-json/ - Each file generates separate test suites
- Shared configuration and helpers
Custom Test Logic
Uncomment and modify the examples in lifecycle hooks:
test.beforeAll(async ({ request }) => {
// Create prerequisite data
const response = await request.post(\`\${baseUrl}/setup\`, {
data: { name: 'Test Environment' },
headers: authHeaders
});
const data = await response.json();
prerequisiteData.setupId = data.id;
});Environment-Specific Configuration
Update env_values.json for different environments:
{
"baseUrl": "https://staging-api.example.com",
"token": "staging-auth-token",
"customConfig": "any-custom-values"
}🚀 Installation Behavior
When you run npm install auto-api-tests-gen-from-swagger, the package:
- Checks your project - Ensures you have a valid Node.js project
- Creates directories - Sets up the required folder structure
- Copies templates - Adds example files and source code
- Generates config - Creates all configuration files
- Updates package.json - Adds necessary scripts
- Shows next steps - Provides clear instructions
🤝 Contributing
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🆘 Support
Made with ❤️ for the API Testing Community
npx playwright test testsThis will show detailed assertion errors, a summary table, and generate an HTML report.
After execution, you will see a summary table in the terminal like this:
| File | Total | Passed | Failed | Flaky | Skipped | |--------------------------------------|-------|--------|--------|-------|---------| | tests/get_policies_.spec.js | 2 | 1 | 1 | 0 | 0 | | tests/post_risks__placements.spec.js | 3 | 2 | 1 | 0 | 0 | | ... | ... | ... | ... | ... | ... | | TOTALS | 88 | 50 | 38 | 0 | 0 |
This summary is provided by the custom summary-table-reporter and helps you quickly review test results across all files.
- View the HTML report:
npx playwright show-report
Configuration
- env_values.json: Set your API base URL and authentication token here. Example:
{ "baseUrl": "https://your-api-url.com", "token": "your-auth-token" } - swagger-json/: Place your Swagger/OpenAPI JSON files here. Update the path in
src/generate-tests.jsif needed.
Requirements
- Node.js (v16+ recommended)
- Playwright (installed via
npm install)
Advanced Usage
- The generator can be extended to support more advanced test flows, custom validation, or dynamic resource creation.
- The custom summary reporter prints a table of test results (file, total, passed, failed, flaky, skipped, totals) in the terminal.
- All test data is generated using
json-schema-fakerfor realistic payloads.
Cleaning Up
To remove all generated tests and test data:
Remove-Item -Recurse -Force .\testdata\*; Remove-Item -Recurse -Force .\tests\*For any issues or feature requests, please contact [email protected].
