Test Overview
This test checks if a variable is correctly assigned and updated during a Postman request flow. It verifies that the variable value changes as expected after the request runs.
This test checks if a variable is correctly assigned and updated during a Postman request flow. It verifies that the variable value changes as expected after the request runs.
pm.test("Variable assignment in flow", function () { // Assign initial value to variable pm.variables.set("counter", 1); // Simulate flow: increment variable let currentValue = pm.variables.get("counter"); pm.variables.set("counter", currentValue + 1); // Verify variable updated correctly pm.expect(pm.variables.get("counter")).to.eql(2); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test environment initialized | — | PASS |
| 2 | Assign initial value 1 to variable 'counter' using pm.variables.set | Variable 'counter' set to 1 in local scope | — | PASS |
| 3 | Retrieve current value of 'counter' using pm.variables.get | Variable 'counter' value is 1 | — | PASS |
| 4 | Increment 'counter' by 1 and assign back using pm.variables.set | Variable 'counter' updated to 2 | — | PASS |
| 5 | Assert that 'counter' equals 2 using pm.expect | Variable 'counter' value is 2 | pm.expect(pm.variables.get("counter")).to.eql(2) | PASS |
pm.variables.set('token', 'abc123') do in a Postman test script?pm.variables.setpm.variables.set(name, value).pm.variables.set('userId', 42); which is correct syntax.pm.variables.set('count', 5);
pm.variables.set('count', pm.variables.get('count') + 3);
console.log(pm.variables.get('count'));pm.variables.set('status', 'active')
pm.variables.get('status')pm.variables.set are local to the current request script and do not persist across requests.pm.environment.set('token', pm.response.json().token);const token = pm.environment.get('token'); uses pm.environment.set and pm.environment.get, which correctly share data between requests.Pass the test and this pattern moves unknown