0
0
Postmantesting~15 mins

Status code assertion in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API returns status code 200 for GET /users
Preconditions (2)
Step 1: Open Postman
Step 2: Create a new GET request to https://api.example.com/users
Step 3: Send the request
Step 4: Observe the response status code
✅ Expected Result: The response status code should be 200
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify response status code is 200
Best Practices:
Use pm.response.to.have.status() for status code assertion
Write clear and simple test scripts
Keep tests independent and focused
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

This test script uses Postman's built-in pm object to check the response status code.

pm.test defines a test with a descriptive name.

pm.response.to.have.status(200) asserts that the response status code equals 200.

This is simple and clear, making it easy to understand and maintain.

Common Mistakes - 3 Pitfalls
Using incorrect assertion method like pm.response.status === 200 without pm.test
Hardcoding status code in multiple places instead of using variables
Not checking status code at all and only validating response body
Bonus Challenge

Now add data-driven testing with 3 different API endpoints and verify each returns status code 200

Show Hint