0
0
Postmantesting~15 mins

What APIs are and why testing them matters in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify GET API returns correct user data
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/users/123
Step 2: Check the response status code is 200
Step 3: Verify the response body contains user ID 123
Step 4: Verify the response body contains user name 'John Doe'
Step 5: Verify the response body contains email 'john.doe@example.com'
✅ Expected Result: The API returns status 200 and the correct user data in JSON format
Automation Requirements - Postman
Assertions Needed:
Status code is 200
Response JSON contains correct user ID
Response JSON contains correct user name
Response JSON contains correct email
Best Practices:
Use environment variables for API base URL
Use test scripts in Postman to assert response data
Keep tests independent and repeatable
Validate both status code and response body content
Automated Solution
Postman
pm.test('Status code is 200', function () {
    pm.response.to.have.status(200);
});

const responseJson = pm.response.json();
pm.test('User ID is 123', function () {
    pm.expect(responseJson.id).to.eql(123);
});

pm.test('User name is John Doe', function () {
    pm.expect(responseJson.name).to.eql('John Doe');
});

pm.test('User email is john.doe@example.com', function () {
    pm.expect(responseJson.email).to.eql('john.doe@example.com');
});

This Postman test script checks the API response after sending a GET request.

First, it verifies the status code is 200, meaning the request succeeded.

Then, it parses the JSON response and checks that the user ID, name, and email match the expected values.

Using pm.test and pm.expect ensures clear, readable assertions that will show pass or fail in the Postman test results.

This approach helps confirm the API returns correct data and is working as expected.

Common Mistakes - 3 Pitfalls
Not checking the status code before validating response data
Hardcoding API URLs directly in tests
Not parsing JSON response before accessing fields
Bonus Challenge

Now add data-driven testing with 3 different user IDs and verify their data

Show Hint