0
0
Postmantesting~15 mins

Local variables in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify usage of local variables in Postman test script
Preconditions (2)
Step 1: Open the GET request in Postman
Step 2: Go to the Tests tab
Step 3: Declare a local variable named 'responseTime' and assign it the value of pm.response.responseTime
Step 4: Write an assertion to check that 'responseTime' is less than 2000 milliseconds
Step 5: Send the request
Step 6: Observe the test results
✅ Expected Result: The test passes if the response time is less than 2000 ms, confirming the local variable 'responseTime' was correctly used in the test script
Automation Requirements - Postman Sandbox JavaScript
Assertions Needed:
Assert that the local variable 'responseTime' is less than 2000
Best Practices:
Use local variables inside the test script scope
Use pm.response object to access response details
Use pm.test and pm.expect for assertions
Automated Solution
Postman
pm.test('Response time is less than 2000ms', () => {
    let responseTime = pm.response.responseTime;
    pm.expect(responseTime).to.be.below(2000);
});

This test script declares a local variable responseTime inside the test function. It assigns the response time from pm.response.responseTime. Then it asserts that this value is below 2000 milliseconds using pm.expect. This shows how to use local variables in Postman test scripts to store and reuse response data within the test scope.

Common Mistakes - 3 Pitfalls
Declaring variables outside the test function and expecting them to persist across requests
Using global or environment variables instead of local variables for temporary data
Not using pm.expect for assertions and instead using console.log to check values
Bonus Challenge

Now add tests to check that the response status code is 200 and the response time is less than 2000ms using local variables

Show Hint