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 |
collection variables in Postman?api_key inside a request URL in Postman?base_url set to https://api.test.com, what will be the full URL for this request?GET {{base_url}}/userstoken but your request still sends Authorization: Bearer {{token}} literally without replacing it. What is the likely cause?token was not saved properly -> Option Buser_id in a request body JSON like this:{ "id": "{{user_id}}", "action": "delete" }id as a number, not a string. How should you fix the request body to send user_id as a number?Pass the test and this pattern moves unknown