Verify POST request creates a new user successfully
Preconditions (3)
✅ Expected Result: Response status code is 201 Created and response body contains the new user's id, name as 'John Doe', and email as 'john.doe@example.com'
pm.test('Status code is 201', () => { pm.response.to.have.status(201); }); const responseJson = pm.response.json(); pm.test('Response has id field', () => { pm.expect(responseJson).to.have.property('id'); }); pm.test('Name is John Doe', () => { pm.expect(responseJson.name).to.eql('John Doe'); }); pm.test('Email is john.doe@example.com', () => { pm.expect(responseJson.email).to.eql('john.doe@example.com'); });
This Postman test script checks the response after sending a POST request to create a user.
First, it verifies the status code is 201, which means the user was created successfully.
Then, it parses the response body as JSON to access its fields.
It asserts that the response contains an 'id' field, confirming the user has an identifier.
Finally, it checks that the 'name' and 'email' fields in the response match the data sent in the request.
These steps ensure the POST request worked as expected.
Now add data-driven testing with 3 different user data inputs to verify POST request creates users correctly
POST request in API testing?pm.test('Status code is 201', () => {
pm.response.to.have.status(201);
});
pm.test('Response has userId', () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('userId');
});userId in the JSON?pm.test('Check success message', () => {
pm.expect(pm.response.json().message).to.eql('Success');
});TypeError: Cannot read property 'message' of undefined. What is the most likely cause?pm.response.json() returned undefined, so accessing .message fails.name and email. Which Postman test script correctly verifies the server response includes the same email you sent and a status code 201?JSON.parse(pm.request.body.raw) to access email for comparison.res.email with req.email, ensuring they match.Pass the test and this pattern moves unknown