Skip to content

Latest commit

 

History

History
134 lines (108 loc) · 2.84 KB

README.md

File metadata and controls

134 lines (108 loc) · 2.84 KB

Specification-driven REST API testing

Build Status Coverage Status

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 = require('swagger-test');

Parse a Swagger specification (e.g. from a Web URL, or the local file system):

var swaggerSpec = // load a Swagger specification as a JavaScript object
var xamples = swaggerTest.parse(swaggerSpec);

The xamples array contains a sequence of request/response pairs. Test them against your service:

var preq = require('preq');

describe('specification-driven tests', function () {
  xamples.forEach(function (xample) {
    it(xample.description, function() {
      return preq[xample.request.method](xample.request)
      .then(function (response) {
        assert.deepEqual(response, xample.responses[response.status]);
      });
    });
  });
});

Test generation

Tests are generated in one of two ways:

  1. Directly from the the x-amples extension to the Swagger specification
  2. 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.parse(spec, { inferXamples: true });