6

I want to verify the response data as a test using Postman. For example, given the response below:

{
  "data": {
    "id": 2,
    "first_name": "Janet",
    "last_name": "Weaver",
    "avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
  }
}

How do I verify this response body as a test?

4 Answers 4

7

As @Mache says, you search for each value.

var jsonData = pm.response.json(); 
pm.test("Verify Json values", function () { 
    pm.expect(jsonData.data.id).is.to.equal(2); 
    pm.expect(jsonData.data.first_name).is.to.equal("Janet"); 
    pm.expect(jsonData.data.last_name).is.to.equal("Weaver"); 
    // and so on and so on
});

The better option:

Create test cases for each assertion

var jsonData = pm.response.json(); 
pm.test("Verify data ID", function () { 
        pm.expect(jsonData.data.id).is.to.equal(2); 
    });
pm.test("Verify first_name", function () { 
        pm.expect(jsonData.data.first_name).is.to.equal("Janet"); 
    });
pm.test("Verify last_name", function () { 
        pm.expect(jsonData.data.last_name).is.to.equal("Weaver"); 
    });

Another option

This will give you a better view on what actually went wrong or is not present on your json response body.

If you really wish to compare the full body, you can create a variable with the expected outcome in a pre-request script like so:

var expectedJsonBody =
{
  "data": {
    "id": 2,
    "first_name": "Janet",
    "last_name": "Weaver",
    "avatar":"https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
  }
}

pm.environment.set("address", JSON.stringify(expectedJsonBody));

in your request test, you compare the response body with the variable.

2

To expand on the answer from @decypher Grouped tests will fail if only one test fails Individual pm.tests will create a higher number of tests in the reports

But you can add a try/catch for each check within a test case like this, and it would throw the error and carry on with subsequent tests;

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

const response = pm.response.json(); 

//Test to FAIL
try{pm.expect(response).to.have.nested.property("category.name", "WRONG DATA");  
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

//Test to PASS
try{pm.expect(response).to.have.nested.property("category.id", 0);
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

//Test to PASS
try{pm.expect(response).to.have.property("id", 0);
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

//Test to FAIL
try{pm.expect(response).to.have.property("name", "WRONG DATA");
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

//Test to PASS
try{pm.expect(response).to.have.property("status", "available");
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

});

2 tests fail and the rest pass.

1

After the obvious - Status code is 2xx you need to add tests based on content:

var jsonData = pm.response.json(); 
pm.test("Verify first_name", function () { 
    pm.expect(jsonData.first_name).is.to.equal("Janet"); 
});

You can check this answer

2
1

What if the response body returns few id's? few users? how can I test the response to find the exact user I am looking for in the response body? Let's say there is 16 users in the response and only one has first_name: Paul, I want to test if there is a user with Paul as a first_name and return his id? This keep testing only first user.

pm.test('Find duplicates', () => { var expectedValue = data.FirstName; const responseJson = JSON.parse(responseBody); pm.response.to.have.body(expectedValue);

});

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.