0
0
Postmantesting~15 mins

Mock server limitations in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify mock server returns correct response and handles unsupported methods
Preconditions (3)
Step 1: Send a GET request to the mock server endpoint /user
Step 2: Verify the response status code is 200
Step 3: Verify the response body contains the expected JSON data
Step 4: Send a POST request to the mock server endpoint /user
Step 5: Verify the response status code is 404 or appropriate error indicating unsupported method
✅ Expected Result: The mock server returns the correct JSON response for GET /user with status 200, and returns an error status for unsupported POST /user request
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 200 for GET /user
Response body matches expected JSON for GET /user
Response status code is 404 or error for POST /user
Best Practices:
Use pm.test and pm.response assertions
Use environment variables for mock server URL
Separate tests for different HTTP methods
Check both status code and response body
Automated Solution
Postman
const mockServerUrl = pm.environment.get('mockServerUrl');

// Test GET /user
pm.test('GET /user returns 200 and expected JSON', async () => {
    const getResponse = await pm.sendRequest({
        url: `${mockServerUrl}/user`,
        method: 'GET'
    });
    pm.expect(getResponse).to.have.property('code', 200);
    pm.expect(getResponse.json()).to.deep.equal({"id": 1, "name": "John Doe"});
});

// Test POST /user unsupported method
pm.test('POST /user returns error status for unsupported method', async () => {
    const postResponse = await pm.sendRequest({
        url: `${mockServerUrl}/user`,
        method: 'POST',
        body: {
            mode: 'raw',
            raw: JSON.stringify({"id": 2, "name": "Jane"})
        }
    });
    pm.expect(postResponse.code).to.be.oneOf([404, 405]);
});

This Postman test script uses pm.sendRequest to send HTTP requests to the mock server URL stored in an environment variable.

First, it sends a GET request to /user and asserts the response status code is 200 and the JSON body matches the expected user data.

Second, it sends a POST request to the same endpoint, which is unsupported by the mock server, and asserts the response status code is either 404 or 405, indicating the method is not allowed.

Using pm.test groups assertions clearly, and pm.expect checks the response properties. This approach verifies mock server limitations on supported HTTP methods.

Common Mistakes - 3 Pitfalls
Hardcoding the mock server URL inside the test script
Not checking the response status code for unsupported methods
Using synchronous pm.sendRequest calls without waiting for response
Bonus Challenge

Now add data-driven testing with 3 different user IDs for GET /user endpoint

Show Hint