Automated RESTful API Testing Using SwaggerAPI
npm install swagger-testing
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.');
}
});
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);
});
});
// 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
});
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);
});
});
To make a new build
npm run build
To run the test
npm test