Verify usage of local variables in Postman test script
Preconditions (2)
✅ 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
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.
Now add tests to check that the response status code is 200 and the response time is less than 2000ms using local variables
token with value abc123 in Postman?pm.variables.set to assign values.pm.environment.set and pm.globals.set set environment and global variables, not local. pm.local.set is invalid.pm.variables.set('count', 5);
pm.variables.set('count', pm.variables.get('count') + 3);
console.log(pm.variables.get('count'));pm.variables.set('user', 'Alice');
console.log(pm.variables.get('username'));pm.variables.set are accessible throughout the request execution, including multiple tests.pm.variables.get to retrieve the local variable value within the same request.Pass the test and this pattern moves unknown