Skip to content

anyarms/swagger-test

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Specification-driven REST API testing

Build Status Coverage Status

Quick start

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.response);
      });
    });
  });
});

Usage

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": [
    {
      "request": {
        "params": {
          "id": "fido4"
        }
      },
      "response": {
        "status": 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 });

About

Specification-driven REST API testing

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 100.0%