0
0
Postmantesting~15 mins

Using mock server URL in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API response using Postman mock server URL
Preconditions (2)
Step 1: Open Postman
Step 2: Create a new GET request
Step 3: Set the request URL to the mock server URL for the /user endpoint
Step 4: Send the request
Step 5: Verify the response status code is 200
Step 6: Verify the response body contains the expected user data
✅ Expected Result: The response status code is 200 and the response body matches the predefined mock user data
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 200
Response body contains expected user data fields and values
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect with JSON parsing for response body validation
Keep test scripts simple and readable
Use environment variables for mock server URL
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body has expected user data', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('id');
    pm.expect(jsonData).to.have.property('name', 'John Doe');
    pm.expect(jsonData).to.have.property('email', 'john.doe@example.com');
});

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

The second test parses the JSON response body and verifies that it contains the expected user data properties: id, name with value 'John Doe', and email with value 'john.doe@example.com'.

Using pm.response.to.have.status and pm.expect ensures clear and readable assertions. This script runs automatically after the request is sent to the mock server URL.

Common Mistakes - 3 Pitfalls
Hardcoding the mock server URL directly in the request
Not checking the response status code before validating the body
Using string matching instead of JSON property checks for response validation
Bonus Challenge

Now add data-driven testing with 3 different mock user responses

Show Hint