Postman Keywords
Common Postman Scripting Keywords
pm.test("name", function(){...})— defines a named test with a pass/fail assertion.pm.response.to.have.status(200)— asserts the response status code.pm.expect(value).to.eql(expected)— general-purpose assertion comparing actual vs expected.pm.response.json()— parses the response body as JSON for further checks.pm.environment.set("key", value)/pm.environment.get("key")— set or read environment variables.pm.globals.set("key", value)— sets a global variable accessible across environments.
Why These Matter
These keywords are the building blocks for turning manual Postman requests into automated, self-verifying tests.
More Objects You Will Use Often
| Keyword | Purpose |
|---|---|
pm.response.code | The numeric status code, useful in conditions |
pm.response.responseTime | Time taken in milliseconds |
pm.response.headers.get("Content-Type") | Reads a response header |
pm.response.text() | Body as plain text |
pm.request.url, pm.request.headers | Details of the request being sent |
pm.collectionVariables.set() / .get() | Variables shared by every request in one collection |
pm.variables.get() | Reads a variable from whichever scope has it, following Postman's priority order |
pm.sendRequest() | Sends an extra request from inside a script |
pm.execution.setNextRequest() | Changes which request runs next in the Collection Runner (older scripts use postman.setNextRequest()) |
A Complete Test Script Example
Paste this into the Scripts > Post-response tab (called "Tests" in older Postman versions) of a login request. It checks the response and saves the token for later requests.
pm.test("Status is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response is fast enough", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
pm.test("Body contains a token", function () {
const body = pm.response.json();
pm.expect(body).to.have.property("token");
pm.expect(body.token).to.be.a("string").and.not.empty;
pm.environment.set("authToken", body.token);
});
pm.test("JSON content type", function () {
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});
Later requests can then use {{authToken}} in an Authorization header, so you never paste tokens by hand.
The pm.expect style comes from the Chai assertion library, so phrases such as to.be.below, to.include, to.have.lengthOf and to.be.oneOf all work. Chaining them reads almost like a sentence, which keeps tests easy to review.
To check every item in an array, loop inside a single test:
pm.test("Every product has a price", function () {
pm.response.json().forEach(p => pm.expect(p.price).to.be.a("number"));
});
Pre-request Scripts
Scripts can also run before a request is sent. A common use is generating unique test data so repeated runs do not clash.
const stamp = Date.now();
pm.collectionVariables.set("uniqueEmail", `user${stamp}@example.com`);
The request body can then use "email": "{{uniqueEmail}}".
Running Scripts Outside the App
Export the collection and environment as JSON files and run them with Newman, Postman's command-line runner. This is how Postman tests join a CI pipeline. The junit reporter writes an XML report that tools such as Jenkins or Azure DevOps can display as a test results page, and a non-zero exit code fails the build automatically when any test fails.
npm install -g newman
newman run api-tests.postman_collection.json -e qa.postman_environment.json --reporters cli,junit
Habits for Clean Postman Scripts
- Give every
pm.testa clear name; it appears in reports and helps others see what failed. - Keep one idea per test so a failure points to a single cause.
- Guard JSON parsing: if a server error returns HTML,
pm.response.json()throws, so check the status first. - Store base URLs and credentials in environments, not inside requests.
- Mark secrets as secret-type variables and avoid committing environment files that contain them.
- Put shared helper code at collection level so it runs for every request.
- Remove temporary variables you created in scripts with
pm.environment.unset("key")once they are no longer needed.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.