pm.test('Status code is 200', () => {
pm.response.to.have.status(200);
});
const expectedStubResponse = {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
};
pm.test('Response matches stub data', () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.eql(expectedStubResponse);
});
// For mock server test, update expectedMockResponse accordingly
const expectedMockResponse = {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
};
pm.test('Response matches mock data', () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.eql(expectedMockResponse);
});This Postman test script checks that the response status code is 200, which means the request was successful.
It then compares the JSON response body to the expected stub data using pm.expect().to.eql() to ensure the stub returns the fixed data.
Similarly, it compares the response to the expected mock data to verify the mock server returns the correct response.
Using environment variables for URLs allows easy switching between stub and mock endpoints.
Each test has a clear name to explain what it verifies.