My primary focus will be to make API Calls and to verify Status Code and Json response.If you know any library which makes this task easier please answer. As of now I am using
- Postman-Newman
- Apache HTTPClient
- Jackson & Gson Parser
- RestAssured
My primary focus will be to make API Calls and to verify Status Code and Json response.If you know any library which makes this task easier please answer. As of now I am using
If you are looking to verify JSON response, there is a library called postman-bdd for Postman
, which lets you write BDD styled
test, using the chai-mocha
syntax (but can also be used to write jasmine styled
syntax). I have used this library for almost 2 months now and it's pretty handy.
Taken directly from the library's npm page -
Postman's built-in test framework uses a boolean-flag syntax for testing, like this:
tests['The correct response code was returned'] = responseCode.code === 200;
tests['The Location header is set'] = postman.getResponseHeader('Location');
tests['The Content-Type is JSON'] = postman.getResponseHeader('Content-Type') === 'application/json';
tests['The response has an ID property'] = JSON.parse(responseBody).id = 12345;
But Postman BDD allows you to use BDD syntax to structure your tests and fluent Chai-JS syntax to write assertions. So the above test suite could look like this instead:
describe('Get customer info', () => {
it('should return a 200 response', () => {
response.should.have.status(200);
});
it('should set the Location header', () => {
response.should.have.header('Location');
});
it('should return a JSON response', () => {
response.should.be.json;
});
it('should return the correct customer', () => {
response.body.should.have.property('id', 12345);
});
});
Apart from this, you can use this library to create custom assertions, using a plugin globals.myCustomAssertions
, and then use it in your tests.
What more, you can define before
and after
hooks that you can use in your API testing, to have something done only once pre or post the test-suite.