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.

  1. Go to your POST request created in the previous lab.
  2. click on Tests.
  3. 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);
    });
  4. 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.
  5. Click Save, then Send.
  6. 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.


  7. 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);
    });
  8. Save and send the request. The test results indicates now that the test fails since the returned status code is 200 and not 404.
teaching_assistant/api/postman_test.txt · Last modified: 2021/09/15 15:29 by Nour
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0