Test Overview
This test checks a simple API workflow in Postman where a user is created, then retrieved, and finally deleted. It verifies that each step completes successfully and the data matches expectations.
This test checks a simple API workflow in Postman where a user is created, then retrieved, and finally deleted. It verifies that each step completes successfully and the data matches expectations.
pm.test("Create User - Status 201", function () { pm.response.to.have.status(201); const jsonData = pm.response.json(); pm.environment.set("userId", jsonData.id); }); pm.test("Get User - Status 200 and correct user", function (done) { pm.sendRequest({ url: `https://api.example.com/users/${pm.environment.get("userId")}`, method: 'GET' }, function (err, res) { pm.expect(res).to.have.property('code', 200); const user = res.json(); pm.expect(user.id).to.eql(pm.environment.get("userId")); done(); }); }); pm.test("Delete User - Status 204", function (done) { pm.sendRequest({ url: `https://api.example.com/users/${pm.environment.get("userId")}`, method: 'DELETE' }, function (err, res) { pm.expect(res).to.have.property('code', 204); done(); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send POST request to create a new user | API server receives user creation request | Response status is 201 Created | PASS |
| 2 | Extract user ID from response and save to environment variable | User ID stored for next requests | — | PASS |
| 3 | Send GET request to retrieve the created user using saved user ID | API server returns user data | Response status is 200 OK and user ID matches saved ID | PASS |
| 4 | Send DELETE request to remove the created user using saved user ID | API server deletes user | Response status is 204 No Content | PASS |
pm.sendRequest(), which sends a request and handles its response.pm.sendRequest('https://api.example.com/token', (err, res) => {
pm.environment.set('token', res.json().access_token);
});token in environment after this runs?res, which can be parsed as JSON.token to res.json().access_token, which is the access token string from the response.pm.sendRequest('https://api.example.com/data', (err, res) => {
pm.environment.set('dataId', res.json().id);
});
pm.sendRequest('https://api.example.com/details/{{dataId}}', (err, res) => {
console.log(res.json());
});dataId?dataId is set only after the first request completes, so the second request may see it as empty if it runs too soon.userId between them?Pass the test and this pattern moves unknown