chrismitchellonline

Postman Tests

2022-10-16

Use test scripts written in Javascript for your Postman API requests. Monitor if your API is up by checking for HTTP status codes, check for specific data responses, and validate API schemes.

Writing Tests in Postman

To create a test for a request in Postman, navigate to the Tests tab. From this editor you can write your tests in Javascript, and the tests will run after the response is received.


Checking for Status Code 200

For our first test we’ll ensure that our endpoint returns HTTP status code 200. Add the following to your script:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

We could add additional tests to check for other response codes (400, 500, etc), but the most common status code is 200, to ensure that our endpoint is up.

Click Send on your request, and the tests will run. Check the Test Results tab to view the results.


Validating Data Results

In our example we are calling a /users endpoint which will return an array of user objects. Each user object will contain a userId, username, and userType. For example:

[
    {
        "userId":"123456",
        "username":"chris@chris.com",
        "userType":"admin"
    },
    {
        "userId":"654632",
        "username":"john@chris.com",
        "userType":"user"
    },
]

We want to ensure that the userType field is always “admin” or “user” as part of the response. We can loop over our response and check each user object. Add the following test:

pm.test("userType is admin or user", () => {
    const responseJson = pm.response.json();
    responseJson.forEach(item=>{
        pm.expect(item.userType).to.be.a('string');
        pm.expect(item.userType).to.be.oneOf(["admin","user"]);
    })            
});

After clicking Send, we’ll see both of our tests run:


If the response contains any other value for the userType field, the test will fail. For example this response returned “BAD DATA”, which would cause the test to fail:


Validating Schema

We want to ensure that the response for the user object is always in the format we expect. For this test we’ll use a schema test.

First, we’ll need to generate a schema object to test. Use this tool to generate a JSON schema:
Free Online JSON to JSON Schema Converter

Generating a schema for my user object I get the following:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "userId": {
      "type": "string"
    },
    "username": {
      "type": "string"
    },
    "userType": {
      "type": "string"
    }
  },
  "required": [
    "userId",
    "username",
    "userType"
  ]
}

Set this schema object to a variable in Postman, and add the following test which will use the tv4 schema validator object to validate the schema that is returned:

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "userId": {
      "type": "string"
    },
    "username": {
      "type": "string"
    },
    "userType": {
      "type": "string"
    }
  },
  "required": [
    "userId",
    "username",
    "userType"
  ]
}

var jsonResponse = pm.response.json();
pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(jsonResponse, schema)).to.be.true;
});

Clicking Send will run all of our tests from the test script window:


Conclusion

In this article we’ve looked at a few different use cases for adding tests to Postman requests. We can validate the endpoint is up with an HTTP status code check, ensure that the data returned for specific fields is what we expect, and validate the schema returned matches our local schema object.

For more information about Postman tests, including the pm testing object, see here:
Writing Postman Tests

Questions or comments about writing tests in Postman? Leave a comment below!

comments powered by Disqus
Social Media
Sponsor