Swagger Test Code Generator
swagger-test-templates is a Node.js library designed to generate API test code directly from a Swagger 2.0 specification. It currently stands at version 1.6.0, with its most recent listed releases (1.5.0, 1.4.0) indicating ongoing maintenance rather than rapid feature development. The library differentiates itself by allowing users to specify the assertion format (e.g., 'should', 'expect', 'assert') and the HTTP request module (e.g., 'supertest', 'request') for the generated tests. It also includes capabilities for generating load tests and supports custom Handlebars templates for advanced customization of the test output. A key limitation is its exclusive focus on Swagger 2.0, meaning it does not natively support newer OpenAPI 3.x specifications.
Common errors
-
TypeError: stt is not a function
cause The module was imported incorrectly, likely using named import syntax for a default export, or attempting to call a property instead of the root function.fixFor CommonJS, use `const stt = require('swagger-test-templates');`. For ESM, use `import stt from 'swagger-test-templates';`. Then call `stt(swagger, config);`. -
Error: 'assertionFormat' is required
cause The `assertionFormat` property was either missing or undefined in the configuration object passed to `stt`.fixAdd a valid `assertionFormat` property (e.g., 'should', 'expect', or 'assert') to your configuration object: `config: { assertionFormat: 'should', ... }`. -
Error: 'testModule' is required
cause The `testModule` property was either missing or undefined in the configuration object.fixAdd a valid `testModule` property (e.g., 'supertest' or 'request') to your configuration object: `config: { testModule: 'supertest', ... }`. -
Generated test files contain 'undefined' for request bodies/parameters.
cause The `requestData` object provided in the configuration does not match the expected nested structure or the Swagger schema for the operation.fixVerify that your `requestData` object precisely follows the `/endpoint -> operation -> responseCode -> array of data` format, and that the data objects themselves contain the correct keys for parameters or the `body` property for request bodies.
Warnings
- breaking This library explicitly supports Swagger Specification version 2.0. It does not support OpenAPI Specification versions 3.x or later. Attempting to use a 3.x spec will lead to generation failures or incorrect tests.
- gotcha When providing a `templatesPath` for custom Handlebars templates, it functions as an 'all-or-nothing' path. You must copy all default templates to your custom directory; the library does not merge custom templates with its built-in ones.
- gotcha The `requestData` option requires a precise nested object structure matching `/endpoint -> operation -> responseCode -> array of data objects`. Incorrect structuring will result in generated tests sending empty or malformed request bodies/parameters.
- gotcha The `pathName` configuration option expects an array of specific paths. If an empty array `[]` is provided, the library will generate tests for *all* paths defined in the Swagger specification, which might be unintended for large APIs.
Install
-
npm install swagger-test-templates -
yarn add swagger-test-templates -
pnpm add swagger-test-templates
Imports
- stt
import { testGen } from 'swagger-test-templates';import stt from 'swagger-test-templates';
- require('swagger-test-templates')
const stt = require('swagger-test-templates'); - testGen
const tests = stt.testGen(swagger, config);
const tests = stt(swagger, config);
Quickstart
const stt = require('swagger-test-templates');
const fs = require('fs');
// In a real scenario, replace this with a path to your actual Swagger 2.0 JSON file
const swaggerSpec = {
"swagger": "2.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {
"/user": {
"get": {
"responses": {
"200": {"description": "OK"}
}
}
},
"/user/{id}": {
"parameters": [{
"name": "id",
"in": "path",
"required": true,
"type": "string"
}],
"get": {
"responses": {
"200": {"description": "OK"}
}
}
}
}
};
const config = {
assertionFormat: 'should',
testModule: 'supertest',
pathName: ['/user', '/user/{id}'],
pathParams: {
"id": "0123"
},
// Example of a load test configuration for a specific path/operation
loadTest: [{
pathName: '/user',
operation: 'get',
load: {requests: 100, concurrent: 10}
}]
};
// Generates an array of objects containing the test file content and name
const tests = stt(swaggerSpec, config);
// Iterate through generated tests and print them (or write to files)
tests.forEach(testFile => {
console.log(`--- Test File: ${testFile.name} ---\n`);
console.log(testFile.test);
// Example: Writing to a file
// fs.writeFileSync(`${testFile.name}`, testFile.test);
});