0
0
Postmantesting~15 mins

Status codes reading in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify HTTP status codes for API responses
Preconditions (2)
Step 1: Send a GET request to the API endpoint https://jsonplaceholder.typicode.com/posts/1
Step 2: Observe the HTTP status code in the response
Step 3: Send a GET request to the API endpoint https://jsonplaceholder.typicode.com/posts/invalid
Step 4: Observe the HTTP status code in the response
✅ Expected Result: The first request returns status code 200 OK, the second request returns status code 404 Not Found
Automation Requirements - Postman Tests
Assertions Needed:
Verify response status code is 200 for valid request
Verify response status code is 404 for invalid request
Best Practices:
Use pm.response.to.have.status() for status code assertions
Write clear and descriptive test names
Keep tests independent and repeatable
Automated Solution
Postman
pm.test('Status code is 200 for valid post', () => {
    pm.response.to.have.status(200);
});

pm.sendRequest('https://jsonplaceholder.typicode.com/posts/invalid', (err, res) => {
    pm.test('Status code is 404 for invalid post', () => {
        pm.expect(res).to.have.property('code', 404);
    });
});

The first test checks that the response status code for a valid GET request is 200, which means OK.

The second test sends a request to an invalid URL and verifies the status code is 404, meaning Not Found.

Using pm.response.to.have.status() and pm.expect() ensures clear and reliable assertions.

Tests are independent and use Postman's built-in test functions for clarity.

Common Mistakes - 3 Pitfalls
Using incorrect assertion syntax like pm.response.status == 200
Not handling asynchronous requests properly in pm.sendRequest
Hardcoding URLs without verifying they exist
Bonus Challenge

Now add data-driven testing with 3 different API endpoints and verify their status codes

Show Hint