Every Postman test starts with one function: pm.test(). That's it. Two arguments — a name (string) and a function (your assertion). If the assertion passes, you see a green checkmark. If it fails, you see a red X with the error message.
// The structure — memorize this
pm.test("Test name goes here", function () {
// Your assertion goes here
});The first argument is a descriptive name. Write it like you're telling someone what you're checking. "Status code is 200" — not "test1." Good test names make debugging easy. Bad names make you cry at 11 PM when a test fails.
| Object | What It Does | Example |
|---|---|---|
| pm.response | Access the response — status, headers, body, time | pm.response.to.have.status(200) |
| pm.expect() | Chai-style assertion — compare values | pm.expect(value).to.eql(42) |
| pm.response.json() | Parse response body as JSON object | const data = pm.response.json() |
Open any GET request in Postman (use https://jsonplaceholder.typicode.com/posts/1). Go to Scripts > Post-response. Paste this.
// Test 1: Is the status code correct?
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// Test 2: Did we get a response fast enough?
pm.test("Response time is under 1 second", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
// Test 3: Is the body actually JSON?
pm.test("Response is JSON", function () {
pm.response.to.have.header("Content-Type", /json/);
});Click Send. Look at the bottom panel — "Test Results" tab. You should see three green checkmarks. Congratulations, you just wrote automated API tests.
Open Postman. Create a GET request to https://jsonplaceholder.typicode.com/posts/1
Click on the "Scripts" tab, then select "Post-response"
Paste the three test scripts from above
Click "Send" to execute the request
Check the "Test Results" tab at the bottom — you should see 3 green checkmarks
pm.test() is asynchronous. If you use pm.response.json() outside a pm.test() block, it still works — but the test name won't appear in results. Always wrap assertions inside pm.test() for proper reporting.
// Both styles are valid — pick one and stick with it
// Classic function
pm.test("Status is 200", function () {
pm.response.to.have.status(200);
});
// Arrow function (shorter)
pm.test("Status is 200", () => {
pm.response.to.have.status(200);
});Postman has a Snippets panel on the right side of the Scripts tab. It has pre-written test templates you can click to insert. Great for beginners — but learn the syntax yourself. Interviewers will ask you to write tests from memory.
Q: What is pm.test() in Postman?
A: pm.test() is a function used to define test assertions in Postman. It takes two arguments — a test description string and a callback function containing the assertion logic. If the assertion inside passes, the test is marked as passed. If it throws an error, the test fails. Multiple pm.test() blocks can exist in one request, and each one shows as a separate pass/fail result in the Test Results tab.
Key Point: pm.test() takes a name and a function. Write descriptive names. Use pm.response for status/headers, pm.expect() for value assertions, pm.response.json() for body.