Dynamic assertion values let you check test results that change each time. This helps make tests flexible and real.
0
0
Dynamic assertion values in Postman
Introduction
When testing APIs that return timestamps or unique IDs.
When the response data changes with each request, like random quotes or user sessions.
When you want to compare a value to a previous response or environment variable.
When validating data that depends on current date or time.
When you need to check if a value exists or matches a pattern instead of exact text.
Syntax
Postman
pm.test("Test name", function () { let jsonData = pm.response.json(); pm.expect(jsonData.key).to.eql(pm.environment.get("variableName")); });
You use pm.environment.get() or pm.variables.get() to get dynamic values.
Assertions compare response data to these dynamic values instead of fixed ones.
Examples
This test checks if the userId in the response matches the userId saved in environment variables.
Postman
pm.test("Check user ID matches stored ID", function () { let jsonData = pm.response.json(); pm.expect(jsonData.userId).to.eql(pm.environment.get("userId")); });
This test compares the date in the response to today's date dynamically.
Postman
pm.test("Response has current date", function () { let jsonData = pm.response.json(); let today = new Date().toISOString().slice(0, 10); pm.expect(jsonData.date).to.eql(today); });
This test checks that the token field exists and is not empty, useful when token changes every time.
Postman
pm.test("Token exists and is not empty", function () { let jsonData = pm.response.json(); pm.expect(jsonData.token).to.be.a('string').and.not.empty; });
Sample Program
This script sets a dynamic expected user ID in environment variables, then tests if the API response userId matches it.
Postman
pm.environment.set("expectedUserId", "12345"); pm.test("User ID matches expected dynamic value", function () { let jsonData = pm.response.json(); pm.expect(jsonData.userId).to.eql(pm.environment.get("expectedUserId")); });
OutputSuccess
Important Notes
Always set or update dynamic values before running assertions.
Use environment or collection variables to store dynamic data between requests.
Dynamic assertions help tests stay valid even when data changes often.
Summary
Dynamic assertion values make tests flexible and realistic.
Use Postman variables to store and compare changing data.
Helps test APIs that return different data each time.