Specification-driven REST API testing
Quick start
To run swagger-test without writing any JavaScript, install the CLI:
$ npm install -g swagger-test
Then, start your server under test, and pipe your Swagger specification
to swagger-test
:
$ cat swagger.json | swagger-test
response did not match specification
the specification is:
{
"status": 200,
"headers": {
"content-type": "application/json; charset=utf-8"
},
"body": {
"greeting": "Hello, world!"
}
}
the response was:
{
"status": 200,
"headers": {
"content-type": "text/html; charset=utf-8"
},
"body": "Hello, world!"
}
Usage
Load the npm module:
var swaggerTest = ;
Parse a Swagger specification (e.g. from a Web URL, or the local file system):
var swaggerSpec = // load a Swagger specification as a JavaScript objectvar xamples = swaggerTest;
The xamples
array contains a sequence of request/response pairs. Test
them against your service:
var preq = ; ;
Test generation
Tests are generated in one of two ways:
- Directly from the the
x-amples
extension to the Swagger specification - Indirectly by inferring examples from the Swagger specification
Direct test specification
The x-amples
extension allows explicit specification of
request/response pairs:
"/pets/{id}": "get": ... "x-amples": "description": "short description of example; it's used as testcase description" "request": "params": "id": "fido4" "responses": "200": "headers": "content-type": "application/json"
These can be specified for any operation.
Indirect test inference
For cases where an explicit examples don't need to be specified, they are inferred directly from the Swagger operation's specification.
"get": "produces": "application/json" "responses": "200": "description": "Returns all the pets"
To enable indirect test inference, set inferXamples
to true
in the
options
argument to parse()
:
var xamples = swaggerTest;