0
0
Postmantesting~10 mins

Setting variables in scripts in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a variable is correctly set in a Postman script and then used in a subsequent request. It verifies that the variable value is stored and accessible.

Test Code - Postman
Postman
pm.test("Set and use environment variable", function () {
    pm.environment.set("userId", "12345");
    let userId = pm.environment.get("userId");
    pm.expect(userId).to.eql("12345");
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test runner is ready to execute the script-PASS
2Set environment variable 'userId' to '12345' using pm.environment.setEnvironment variables now include userId = '12345'-PASS
3Retrieve environment variable 'userId' using pm.environment.getVariable userId retrieved with value '12345'-PASS
4Assert that retrieved userId equals '12345' using pm.expectAssertion compares expected '12345' with actual '12345'pm.expect(userId).to.eql('12345')PASS
5Test ends successfullyVariable set and verified correctly-PASS
Failure Scenario
Failing Condition: Variable 'userId' is not set or has a different value than expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does pm.environment.set("userId", "12345") do in the script?
ADeletes the environment variable 'userId'
BRetrieves the value of 'userId' from environment variables
CStores the value '12345' in the environment variable named 'userId'
DRuns a test to check if 'userId' equals '12345'
Key Result
Always verify that variables are set before using them in assertions to avoid undefined errors in Postman scripts.