0
0
Postmantesting~15 mins

Creating mock servers in Postman - Automation Script Walkthrough

Choose your learning style9 modes available
Create and test a Postman mock server for a simple API
Preconditions (2)
Step 1: Open Postman and select the collection to mock
Step 2: Click on the three dots next to the collection name and select 'Mock Collection'
Step 3: In the mock server creation dialog, choose 'Save the mock server URL as an environment variable'
Step 4: Click 'Create Mock Server'
Step 5: Copy the mock server URL from the environment variable
Step 6: Send a request to the mock server URL using the same endpoint and method as in the collection
Step 7: Verify the response matches the example response defined in the collection
✅ Expected Result: The mock server is created successfully, and requests sent to the mock server URL return the example response with status code 200
Automation Requirements - Postman test scripts
Assertions Needed:
Verify response status code is 200
Verify response body matches the example response
Best Practices:
Use environment variables to store mock server URL
Use example responses in collection requests for mocking
Write clear and simple test scripts in Postman
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Not using environment variables for the mock server URL
Not defining example responses in the collection requests
Not verifying the response status code in tests
Bonus Challenge

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.

Show Hint