Writing a simple test in Postman
A test is just a simple assertion in which you check a value from a response against an expected value. In this tutorial, we will create a simple test in Postman that checks if an API request is successfully executed without the need to check manually the Body response. It is important to note that tests only run after the request is completed.
-
click on Tests.
Click the snippet
Status code: code is 200. If you don't find it, add the below code manually:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
The first parameter is the test name Status code is 200. The second parameter is the callback function in which you write the assertions related to this test.
Click Save, then Send.
Click the
Test Results tab in the response part. The result shows that this test has passed which means that the request is successfully executed and the response status code is actually 200.
You can make the test fail by changing the value in the test code from 200 to 404:
pm.test("Status code is 404", function () {
pm.response.to.have.status(404);
});
Save and send the request. The test results indicates now that the test fails since the returned status code is 200 and not 404.
Back to top