How to create Tests in Postman

HOME

In this tutorial, we will see how we can create Tests in the Postman.

Consider a sample JSON which has 4 sets of data. This data we will be passing to a POST request.

Create a request in Postman. Below is the URL of the request.

https://reqres.in/api/users

Paste the body of the request in the Body part of the Postman.

Create Tests for the request

Click on the “Tests”. Write the tests as shown below:

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

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

pm.test("The endpoint does not return unexpected status code", () => {
 
  // change 404 to the response code you do not expect
  pm.response.to.not.have.status(404);
});

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("API response contians the expected header", () => {
  pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("Postman");
});

The output of the above test is

pm.test("Value of name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.name).to.eql("Postman");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the name field is Postman.
  pm.expect(response).to.have.property("name", "Postman");

  // the line below checks value of the job field is Test (string).
  pm.expect(response).to.have.property("job", "Test");
});

The output of the above test is

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

The output of the above test is

const responseJson = pm.response.json();
pm.test("Test Data Type of the response", () => {
    
    pm.expect(responseJson.job).to.be.a("string");
    pm.expect(parseInt(responseJson.id)).to.be.a("number");
});

pm.test("The response has all properties", () => {
    //parse the response JSON and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.name).to.eql("Postman");
    pm.expect(responseJson.job).to.be.a('string')
    pm.expect(responseJson.id).to.have.lengthOf(3);
});

Sample Response

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  // the line below checks value of the id field is 2 (number).
  pm.expect(response).to.have.property("id", 2);

  // the line below checks value of the name field is Morty Smith (string).
  pm.expect(response).to.have.property("name", "Morty Smith");

  // the line below checks value of the origin.name field is Earth (C-137) (string).
  pm.expect(response).to.have.nested.property("location.name", "Citadel of Ricks");
});

pm.test("API response contains the expected fields", () => {
  const response = pm.response.json();

  pm.expect(response).to.have.nested.property("episode.3", "https://rickandmortyapi.com/api/episode/4");
});

Leave a comment