/swagger-testing

Automated API Testing Using SwaggerAPI

Primary LanguageTypeScriptOtherNOASSERTION

Swagger Testing

Build Status

Automated RESTful API Testing Using SwaggerAPI

Note: This project is under development and is not ready yet.

Installation

npm install swagger-testing

Usage

Independent

var SwaggerTesting = require('swagger-testing');
var swaggerSpec = require('./swagger.json');

var swagger = new SwaggerTesting(swaggerSpec);

swagger.testOperation({path: '/pet', operation: 'GET'}, function (err) {
  if (!err) {
    console.log('Successfully tested GET /pet');
  }
});

swagger.testCRUD('/user', '/pet', function (err) {
  if (!err) {
    console.log('All CRUD operations for all objects in my API are tested successfully.');
  }
});

In Mocha/Jasmine tests

Use SwaggerTesting in your Mocha tests:

var SwaggerTesting = require('swagger-testing');
var swaggerSpec = require('./swagger.json');

var swagger = new SwaggerTesting(swaggerSpec);

// Automatically test all models
describe('My API', function() {
  it ('tests all objects CRUD operations', function(done){
    swagger.testCRUD('/user', '/pet', done);
  });
});

API

// Automatically test all models
swagger.testCRUD('/user', '/pet');
// Automatically test CRUD resources
swagger.testCRUD('/user');
// Test all non mutating paths
swagger.testAllOperations('GET');
// Test a specific operation
swagger.testOperation({
  path: '/pet',
  operation: 'PUT',
  data: pet
});

A complex flow

describe('CRUD Pet (Manual)', function() {
  var pet = null;

  it('Creates a Pet object', function(done) {
    swagger.testOperation({path: '/pet', operation: 'POST'}, function(err, result) {
      pet = result;
      done();
    });
  });

  it('Reads the created Pet object', function(done) {
    swagger.testOperation({path: '/pet/' + pet.id, operation: 'GET'}, done);
  });

  it('Updates the created Pet object', function(done) {
    pet.name = Math.random().toString(36);

    swagger.testOperation({
      path: '/pet',
      operation: 'PUT',
      data: pet
    }, done);
  });

  it('Deletes the created Pet object', function(done) {
    swagger.testOperation({path: '/pet/' + pet.id, operation: 'DELETE'}, done);
  });
});

Development

To make a new build

npm run build

To run the test

npm test

License

MIT