0
0
Postmantesting~10 mins

Collection variables in Postman - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Postman Test Script
Postman
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');
    });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test script execution begins-PASS
2Retrieve collection variable 'baseUrl' using pm.collectionVariables.getVariable 'baseUrl' is fetched from collection variablesCheck that 'baseUrl' is a non-empty stringPASS
3Send GET request to URL constructed from 'baseUrl' + '/status'Request sent to the server at the constructed URLVerify no error in request and response code is 200PASS
4Check response JSON has property 'status' with value 'ok'Response received with JSON bodyAssert response JSON property 'status' equals 'ok'PASS
5Test endsAll assertions passed-PASS
Failure Scenario
Failing Condition: Collection variable 'baseUrl' is missing or empty, or the GET request returns an error or unexpected response
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check first in the execution trace?
AThat the collection variable 'baseUrl' exists and is a non-empty string
BThat the response status code is 404
CThat the request URL is hardcoded
DThat the response JSON has a 'message' property
Key Result
Always verify that collection variables are properly set before using them in requests to avoid test failures due to missing or incorrect variable values.