Create and test a Postman mock server for a simple API
Preconditions (2)
✅ Expected Result: The mock server is created successfully, and requests sent to the mock server URL return the example response with status code 200
Jump into concepts and practice - no test required
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body matches example', () => { const exampleResponse = pm.collectionVariables.get('exampleResponse'); const responseBody = pm.response.json(); pm.expect(responseBody).to.eql(JSON.parse(exampleResponse)); });
The first test checks that the response status code is 200, which means the mock server responded successfully.
The second test compares the actual response body with the example response stored in a collection variable called 'exampleResponse'. This ensures the mock server returns the expected data.
Using environment and collection variables helps keep the test flexible and maintainable.
Now add data-driven testing by creating multiple example responses for different scenarios and verify the mock server returns the correct response based on request parameters.
{
"method": "GET",
"url": "/user/123",
"response": {
"status": 200,
"body": "{\"id\":123, \"name\": \"Alice\"}"
}
}/user/123?