Test Overview
This test checks if the Postman script correctly generates a current timestamp and verifies its format and value.
Jump into concepts and practice - no test required
This test checks if the Postman script correctly generates a current timestamp and verifies its format and value.
pm.test("Timestamp is generated and valid", function () { const timestamp = Date.now(); pm.environment.set("currentTimestamp", timestamp); pm.expect(timestamp).to.be.a('number'); pm.expect(timestamp).to.be.above(0); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test runner is ready to execute the test script | — | PASS |
| 2 | Generates current timestamp using Date.now() | Timestamp is a numeric value representing milliseconds since Unix epoch | Check that timestamp is a number | PASS |
| 3 | Stores timestamp in Postman environment variable 'currentTimestamp' | Environment variable 'currentTimestamp' holds the timestamp value | — | PASS |
| 4 | Asserts timestamp is greater than zero | Timestamp value is positive and valid | Verify timestamp > 0 | PASS |
| 5 | Test ends | Test completed successfully with all assertions passing | — | PASS |
Date.now() return in Postman scripts?Date.now() returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.pm.environment.set(key, value) is used to save a variable.set with the right syntax.let start = Date.now();
pm.environment.set('startTime', start);
let end = Date.now();
pm.environment.set('endTime', end);
let duration = pm.environment.get('endTime') - pm.environment.get('startTime');
console.log(duration);start and end capture timestamps at two different moments, so end ≥ start.start from end gives the elapsed time in milliseconds, which is positive or zero.pm.environment.set('timeStamp', Date.now)Date.now is a function reference, not the timestamp itself.(), like Date.now().startTime there is correct.startTime from current time gives elapsed time.