0
0
Postmantesting~15 mins

Example requests and responses in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify GET request to fetch user details
Preconditions (2)
Step 1: Open Postman
Step 2: Create a new GET request
Step 3: Enter URL https://api.example.com/users/123
Step 4: Click Send button
Step 5: Observe the response body and status code
✅ Expected Result: Response status code is 200 and response body contains user details with id 123
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify response status code is 200
Verify response body contains user id 123
Verify response body has keys: id, name, email
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect with JSON parsing for body content
Write clear and simple test scripts inside Postman Tests tab
Automated Solution
Postman
pm.test('Status code is 200', function () {
    pm.response.to.have.status(200);
});

pm.test('Response has user id 123', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql(123);
});

pm.test('Response has required keys', function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.all.keys('id', 'name', 'email');
});

The first test checks if the response status code is 200, which means the request was successful.

The second test parses the JSON response and verifies the user id is exactly 123.

The third test ensures the response JSON contains the keys 'id', 'name', and 'email', confirming the expected data structure.

All tests use Postman's pm and pm.expect APIs for clear and reliable assertions.

Common Mistakes - 3 Pitfalls
Not parsing response JSON before accessing properties
Checking status code with pm.response.code instead of pm.response.to.have.status
Hardcoding expected values without verifying response structure
Bonus Challenge

Now add tests for POST request to create a new user with 3 different user data inputs

Show Hint