0
0
Postmantesting~10 mins

Using Chai assertion library in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a GET request to a sample API endpoint and uses the Chai assertion library to verify that the response status is 200 and the response body contains a specific property.

Test Code - Postman with Chai assertions
Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response has userId property", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('userId');
});
Execution Trace - 3 Steps
StepActionSystem StateAssertionResult
1Send GET request to https://jsonplaceholder.typicode.com/posts/1Request sent, waiting for response-PASS
2Check response status code using pm.response.to.have.status(200)Response received with status 200Verify status code is 200PASS
3Parse response JSON and check if it has property 'userId' using pm.expectResponse body parsed as JSON: { userId: 1, id: 1, title: "...", body: "..." }Verify response JSON has property 'userId'PASS
Failure Scenario
Failing Condition: Response status is not 200 or response JSON does not have 'userId' property
Execution Trace Quiz - 3 Questions
Test your understanding
What does the first assertion check in this test?
AThat the request URL is correct
BThat the response body contains 'userId'
CThat the response status code is 200
DThat the response time is less than 200ms
Key Result
Always verify both the HTTP status code and the expected properties in the response body to ensure the API behaves correctly.