Test Overview
This test sends a GET request to a user API endpoint and verifies that the returned user ID matches the expected dynamic value from the response. It checks that the user name is not empty and the status code is 200.
This test sends a GET request to a user API endpoint and verifies that the returned user ID matches the expected dynamic value from the response. It checks that the user name is not empty and the status code is 200.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test("User ID matches dynamic value", function () { const jsonData = pm.response.json(); const expectedUserId = jsonData.data.id; pm.expect(jsonData.data.id).to.eql(expectedUserId); }); pm.test("User name is not empty", function () { const jsonData = pm.response.json(); pm.expect(jsonData.data.name).to.not.be.empty; });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send GET request to https://api.example.com/users/123 | Request sent, waiting for response | — | PASS |
| 2 | Receive response with status code 200 and JSON body containing user data | Response received: {"data":{"id":123,"name":"Alice"}} | Check that status code is 200 | PASS |
| 3 | Extract user ID from response JSON and assign to expectedUserId | expectedUserId = 123 | Verify that response user ID equals expectedUserId | PASS |
| 4 | Check that user name field is not empty | User name is 'Alice' | Verify user name is not empty | PASS |
dynamic assertion values in Postman tests?pm.variables.get('variableName').pm.variables.get('randomId') to get the variable value for comparison.pm.test('Check user ID', () => {
const expectedId = pm.variables.get('userId');
pm.expect(pm.response.json().id).to.eql(expectedId);
});userId is set to 123 and the response JSON is {"id": 124}, what will be the test result?userId is 123, but response JSON has id 124.to.eql(123) vs actual 124 will fail because they are not equal.pm.test('Check token', () => {
const token = pm.variables.get('authToken');
pm.expect(pm.response.json().token).to.eql(token);
});authToken is missing or empty, pm.variables.get('authToken') returns undefined or empty string.prevOrderId. Which Postman test code correctly asserts that the new order ID is different from the previous one?not.eql to assert the new ID is not equal to the previous ID, matching the requirement.Pass the test and this pattern moves unknown