Test Overview
This test checks that a Postman pre-request script correctly prepares data before sending an API request. It verifies that the data is set in the environment and used in the request body.
This test checks that a Postman pre-request script correctly prepares data before sending an API request. It verifies that the data is set in the environment and used in the request body.
pm.test("Pre-request script sets data correctly", () => { // Run the pre-request script manually pm.environment.set('userId', '12345'); // Simulate sending a request using the prepared data const requestBody = { id: pm.environment.get('userId'), name: 'Test User' }; // Assert the userId is set correctly pm.expect(pm.environment.get('userId')).to.eql('12345'); // Assert the request body uses the prepared data pm.expect(requestBody.id).to.eql('12345'); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman environment is clean with no userId set | - | PASS |
| 2 | Pre-request script sets environment variable 'userId' to '12345' | Environment variable 'userId' is now '12345' | Check environment variable 'userId' equals '12345' | PASS |
| 3 | Prepare request body using environment variable 'userId' | Request body contains { id: '12345', name: 'Test User' } | Verify requestBody.id equals '12345' | PASS |
| 4 | Assertions verify environment variable and request body data | Assertions pass confirming data preparation | pm.expect(pm.environment.get('userId')).to.eql('12345') and pm.expect(requestBody.id).to.eql('12345') | PASS |