0
0
Postmantesting~10 mins

Variable scope and precedence in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks how Postman uses variables from different scopes (global, collection, environment, local) and verifies that the variable with the highest precedence is used in the request.

Test Code - Postman Sandbox
Postman
pm.test("Variable scope and precedence test", function () {
    // Set variables in different scopes
    pm.globals.set("var1", "globalValue");
    pm.collectionVariables.set("var1", "collectionValue");
    pm.environment.set("var1", "environmentValue");
    pm.variables.set("var1", "localValue");

    // Access variable in test
    const resolvedVar = pm.variables.get("var1");

    // Assert that the local variable has highest precedence
    pm.expect(resolvedVar).to.eql("localValue");
});
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test script execution begins-PASS
2Set global variable 'var1' to 'globalValue'Global variables include var1=globalValue-PASS
3Set collection variable 'var1' to 'collectionValue'Collection variables include var1=collectionValue-PASS
4Set environment variable 'var1' to 'environmentValue'Environment variables include var1=environmentValue-PASS
5Set local variable 'var1' to 'localValue'Local variables include var1=localValue-PASS
6Retrieve variable 'var1' using pm.variables.get()Variable resolved to 'localValue' due to precedence rulesCheck if resolved variable equals 'localValue'PASS
7Test endsTest completed successfully-PASS
Failure Scenario
Failing Condition: Variable 'var1' resolves to a value other than 'localValue', indicating incorrect scope precedence
Execution Trace Quiz - 3 Questions
Test your understanding
Which variable value does pm.variables.get('var1') return in this test?
AcollectionValue
BenvironmentValue
ClocalValue
DglobalValue
Key Result
Always set and verify variables in the correct scope and use pm.variables.get() to respect Postman's variable precedence rules for reliable test results.