API Testing — POSTMAN — Testing More Than The Status Code

Ryan Craven
6 min readAug 2, 2022

--

POSTMAN is a great tool; by now, I hope you’re comfortable writing and executing API calls. If you aren't familiar with making an API call using POSTMAN, refer to my previous post. However, there's much more than just executing an API and seeing the green 200 OK. In this post, we'll dive into the POSTMAN testing tab.

POSTMAN Tests tab

POSTMAN stores requests in the following hierarchy: Collections, Folders, and Requests. Similarly, scripts are executed in this order as well. The way I see it, the further you go in the hierarchy, the more specific the test should be.

Test scripts execute after the response is received. We will perform a different test at each level to show various examples.

POSTMAN Testing Background

Test scripts use JavaScript syntax. However, knowledge of JavaScript isn't required because POSTMAN provides snippets of pre-written code that you can modify to fit a particular test.

Main Elements of Test Scripts

  1. pm.test(“Name of the test”, function() {
    //This is where you write your response/expect tests
    });
    — Names your test, and the name will show up in the resulting output
    — Returns a boolean that indicates if the test passed or failed
  2. pm.response.to.have.status(200);
    — Validates data returned by the request

3. pm.expect(pm.response.code).to.be.oneOf([201, 202]);
— Similar to pm.response, however, the result message is slightly different

4. console.log("Starting a test");
— Displays the text on the console

Setting up a Collection

  1. Create a new collection by clicking the +New Collection button.

2. Give the Collection a name. I used "Postman Example" for this example.

3. Click on the Tests tab in the CREATE A NEW COLLECTION window.

4. Add a test that verifies the status code returned.

5. Click Create button.

6. Verify the collection was successfully created.

7. Congratulations, you've created your first collection. Unfortunately, tests cannot run because there are no requests.

Setting Up Folders

  1. Click the *** icon next to the collection.

2. From the displayed menu, click on Add Folder.

3. Give the folder a name. In this example, I used Get Requests.

4. Click on the Tests tab in the ADD FOLDER TO POSTMAN EXAMPLE window.

5. For this example, let's say we want to verify all request responses are below a particular time. So, within the Folder Tests tab, write a test that verifies response time is below 200ms.

6. Click Create to create the folder

7. Verify the folder was created.

Creating Requests

  1. Click the *** icon next to Get Requests.

2. From the displayed menu, click on Add Request

3. I copied the GET — LIST USERS request covered in the previous blog for this example. If you didn't read my previous blog post, make a GET request and add ( https://reqres.in/api/users?page=2 ) to the Enter request URL text box).

4. With the GET — LIST USERS request selected, click on the Tests tab

5. In the Tests tab, we will write a test script that verifies the JSON's response in a specific format with correct data values.

Testing In Action

With the Test Scripts written at the Collection, Folder, and Request level, let's run the Collection (Postman Example) to see the results.

  1. Click the arrow icon next to the Collection name.

2. Click the Run button (this assumes you have Postman downloaded on your PC, if not, click View in Web).

3. (Desktop Version) A Collection Runner window is opened. The request should already be selected, so click Run Postman Example and check your results.

4. (Web Version) Find the Postman Example collection, click the more action icon ( *** ), and select Run collection. A Runner tab should be displayed and selected. Next, click the blue Run Postman Example button to run the collection and review the results.

I've attached the POSTMAN collection from the previous section. Download and import the collection via the import button in the Header Bar. There are notes attached to the collection folder for further clarification. I recommend editing the test scripts and executing the APIs to become familiar with different ways to test APIs.

Summary

Postman doesn't just validate APIs but can also be a testing tool. I've touched on test scripting and demonstrated how to write tests at each hierarchy and run the tests. Keep in mind; that the closer you get to the request level, the more refined and specific your test shall be. Overall, Postman is a versatile tool that every quality assurance engineer should know and understand.

To stay updated on future posts, subscribe to my newsletter below:

If you enjoy my articles, be sure to check out my Etsy Bookstore

--

--

Ryan Craven

Sr Quality Engineer & AI Enthusiast • Writing on AI, Tech & Testing • Read my articles for free and join my newsletter: https://ryancraventech.substack.com