Test Overview
This test verifies that a Postman collection variable is correctly set and used in a request. It checks that the variable value is accessible and the request returns the expected response.
This test verifies that a Postman collection variable is correctly set and used in a request. It checks that the variable value is accessible and the request returns the expected response.
pm.test("Collection variable is set and used correctly", function () { // Get the collection variable 'baseUrl' const baseUrl = pm.collectionVariables.get("baseUrl"); // Check that the variable is not undefined or empty pm.expect(baseUrl).to.be.a('string').and.not.empty; // Send a GET request to the baseUrl + '/status' pm.sendRequest(baseUrl + "/status", function (err, res) { pm.expect(err).to.be.null; pm.expect(res).to.have.property('code', 200); pm.expect(res.json()).to.have.property('status', 'ok'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test script execution begins | - | PASS |
| 2 | Retrieve collection variable 'baseUrl' using pm.collectionVariables.get | Variable 'baseUrl' is fetched from collection variables | Check that 'baseUrl' is a non-empty string | PASS |
| 3 | Send GET request to URL constructed from 'baseUrl' + '/status' | Request sent to the server at the constructed URL | Verify no error in request and response code is 200 | PASS |
| 4 | Check response JSON has property 'status' with value 'ok' | Response received with JSON body | Assert response JSON property 'status' equals 'ok' | PASS |
| 5 | Test ends | All assertions passed | - | PASS |