Postman Writing Tests
Postman lets you write automated tests that check whether an API response is correct. After each request runs, these tests verify that the data, status code, and structure meet your expectations. You write tests in JavaScript using Postman's built-in pm object.
Why Automated Tests Matter
Imagine testing a login API manually every day. You would send the request, read the response, check the status code, and look for a token. Now imagine doing this for 50 endpoints. Automated tests do all of this in seconds and tell you exactly which check failed and why.
Without Tests: With Tests:
Send request Send request
Read response Tests run automatically
Check status code ✅ Status is 200
Check response body ✅ Token exists in body
Remember to check headers ❌ Response time exceeded 500ms
Note findings Results logged and saved
Repeat for every request
Where to Write Tests in Postman
Click the Scripts tab (previously called Tests tab) below the URL bar in any request. Choose the Post-response section. Code you write here runs automatically after each response arrives.
The pm Object
Postman provides a global pm object in all scripts. It gives you access to the request, the response, variables, and a testing interface.
| pm property / method | What It Gives You |
|---|---|
| pm.response.code | The HTTP status code number (e.g., 200) |
| pm.response.json() | The response body parsed as a JavaScript object |
| pm.response.text() | The response body as a plain string |
| pm.response.headers.get("key") | A specific response header value |
| pm.response.responseTime | Time in milliseconds the server took to respond |
| pm.test("name", fn) | Registers and runs a test |
| pm.expect(value) | Starts a Chai assertion |
Write Your First Test
Open a GET request to https://jsonplaceholder.typicode.com/users/1 and go to the Scripts → Post-response section. Write this test:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});Click Send. The Test Results tab appears in the response panel and shows a green checkmark next to "Status code is 200". A red cross appears if the test fails.
Common Tests You Will Write
Check Status Code
pm.test("Status is 200 OK", function () {
pm.response.to.have.status(200);
});Check a Field in the Response Body
pm.test("User name exists", function () {
const data = pm.response.json();
pm.expect(data.name).to.be.a("string");
});Check a Specific Value
pm.test("User ID is 1", function () {
const data = pm.response.json();
pm.expect(data.id).to.equal(1);
});Check Response Time
pm.test("Response time is under 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});Check a Response Header
pm.test("Content-Type is JSON", function () {
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});Testing an Array Response
When an API returns a list of items, you can check the list is not empty and that items have the right shape:
pm.test("Posts list is not empty", function () {
const posts = pm.response.json();
pm.expect(posts).to.be.an("array");
pm.expect(posts.length).to.be.above(0);
});
pm.test("Each post has a title", function () {
const posts = pm.response.json();
posts.forEach(function (post) {
pm.expect(post).to.have.property("title");
});
});Use the Snippet Sidebar
Postman's Scripts panel has a Snippets section on the right side. It lists pre-written test templates. Click any snippet to insert it into your test area instantly. Common snippets include:
- Status code: code is 200
- Response body: JSON value check
- Response time is less than 200ms
- Response body: contains string
Snippets speed up writing and help you learn the correct syntax.
Test Results Panel
After sending a request with tests, click the Test Results tab in the response panel. Each test shows:
- A green PASS badge if the assertion succeeded
- A red FAIL badge with an error message if something went wrong
Test Diagram – Full Flow
Request Sent
│
▼
Server Responds (200 OK, JSON body)
│
▼
Post-response script runs
│
├── ✅ Status is 200 → PASS
├── ✅ Name is a string → PASS
└── ❌ ID equals 2 → FAIL (actual: 1)
│
▼
Test Results tab shows summary
Summary
Tests in Postman run automatically after each response using JavaScript in the Post-response section. The pm.test function defines a test and pm.expect checks values using Chai assertions. Tests verify status codes, response fields, values, headers, and response times. Use the Snippets panel to insert common test templates quickly and speed up your workflow.
