0
0
Postmantesting~15 mins

Tests tab and pm.test() in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API response status and body using pm.test() in Postman Tests tab
Preconditions (2)
Step 1: Open Postman and create a new GET request
Step 2: Enter the URL https://jsonplaceholder.typicode.com/posts/1
Step 3: Go to the Tests tab below the request URL field
Step 4: Write pm.test() scripts to check the response status is 200
Step 5: Write pm.test() scripts to check the response body contains userId equal to 1
Step 6: Send the request by clicking the Send button
Step 7: Observe the test results in the Tests tab output
✅ Expected Result: The response status code is 200 and the test passes. The response body contains userId equal to 1 and the test passes. Both tests show as passed in the Tests tab.
Automation Requirements - Postman Tests tab scripting with pm.test()
Assertions Needed:
Verify response status code is 200
Verify response JSON body has userId equal to 1
Best Practices:
Use pm.test() to group assertions with descriptive names
Use pm.response.to.have.status() for status code assertion
Use pm.expect() with JSON parsing for body content assertion
Keep tests clear and readable
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body has userId equal to 1', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData.userId).to.eql(1);
});

The first pm.test() checks that the response status code is exactly 200 using pm.response.to.have.status(200). This ensures the API call was successful.

The second pm.test() parses the JSON response body with pm.response.json() and then uses pm.expect() to assert that the userId field equals 1. This confirms the response data is correct.

Grouping assertions inside pm.test() gives clear, named test results in Postman’s Tests tab, making it easy to see which checks passed or failed.

Common Mistakes - 3 Pitfalls
Not using pm.test() and writing assertions outside it
Using pm.response.code instead of pm.response.to.have.status()
Not parsing JSON response before accessing fields
Bonus Challenge

Now add data-driven testing with 3 different post IDs (1, 2, 3) to verify each response has the correct userId.

Show Hint