-
Notifications
You must be signed in to change notification settings - Fork 2
Common Test
There will be test that you add to almost every call you make when testing an API these are test which I prefer to us Reusable Codefor.
One of the first test you will run is to see if you get a valid response. Your API documentation should state what each return is for. If you don't know then test for a 200 as that is the most common successful code.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
If you want to learn more about the codes you can go HERE and read about them. Common error ones are 400 for a bad request and 500 for internal server error. The most popular of course is 404 for not found.
Next you want to check if the API is coming back in a decent amount of time. Most common is 200ms but I like to give it some breathing room with 500ms. Sometimes if alot of data is rendered or your application is in no hurry you can go to 2000ms or 2 seconds.
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
This checks if the body exist and if it is valid. You might ask if the above test that. It does not. If what is returned is not valid Json than it won't error. Also nothing is valid Json.
pm.test(environment.requestName + "Response must be valid and have a body", function () {
pm.response.to.be.json; // this assertion checks if a body exists
});
Here we test to see that the code does not error. You want to run this normally with a 200 because you don't want to get back json that is an error.
pm.test(environment.requestName + "Response does not error", function () {
pm.response.to.not.be.error;
pm.response.to.not.have.jsonBody("error");
});
However with a 400 you want to get back an error so this is what you would use.
pm.test(environment.requestName + "Has correct schema", function() {
pm.response.to.be.error;
});