Postman Tests: Write & Run API Response Tests
Postman lets you write automated tests that run against each API response, checking status codes, response times, JSON fields, headers, and more. This guide walks you through writing test scripts in the Tests tab, running them with the built-in test runner or collection runner, and reading the results. You don’t need any prior scripting experience, but basic JavaScript knowledge helps.
What You’ll Need
- Postman installed (desktop app or web version)
- An API endpoint you can call (a public one like
https://jsonplaceholder.typicode.com/users/1works for practice) - A Postman request already set up (method, URL, headers, body if needed)
Step-by-Step
- Open your request – In Postman, select the request you want to test. If you don’t have one, create a new request, enter a URL (e.g.,
https://jsonplaceholder.typicode.com/users/1), and set the method to GET. - Go to the Tests tab – Below the URL bar, you’ll see tabs: Params, Authorization, Headers, Body, Pre-request Script, and Tests. Click Tests.
- Write a basic test – In the editor, type:
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });This checks that the response status is exactly 200.
- Check response time – Add a test that ensures the response is fast enough:
pm.test("Response time is less than 500ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); });Adjust the threshold to your needs.
- Validate the response body – Use
pm.response.json()to parse the JSON body and check fields:pm.test("User has a name", function () { var data = pm.response.json(); pm.expect(data.name).to.be.a('string'); });For arrays, you can check length:
pm.expect(data.length).to.eql(10); - Check response headers – Verify a header exists and has a value:
pm.test("Content-Type is JSON", function () { pm.expect(pm.response.headers.get('Content-Type')).to.include('application/json'); }); - Save the request – Click Save (or Ctrl/Cmd+S) to keep your tests with the request.
- Run the test – Click the Send button. The response area now shows a Test Results tab (next to Body, Cookies, Headers). Click it to see each test pass or fail.
- Run multiple requests with the Collection Runner – If you have a collection, click the collection name in the left sidebar, then click Run (or the play icon). In the runner, select your collection, choose an environment if needed, and click Run Collection. You’ll see a summary of all tests across all requests.
- Use the command line (Newman) – To run tests from CI or terminal, install Newman (
npm install -g newman) and run:newman run my-collection.jsonExport your collection first (right-click collection → Export). Newman will report pass/fail counts.
Common Mistakes
- Forgetting to save – If you don’t save the request, your tests won’t be included when you run the collection.
- Using
pm.response.to.have.statuswith a string – The status must be a number, not a string.status(200)works,status("200")fails. - Assuming the response is JSON – If the API returns XML or plain text,
pm.response.json()will throw an error. Checkpm.response.headers.get('Content-Type')first. - Testing too much in one test – Keep each test focused on one assertion. This makes failures easier to debug.
- Hardcoding values that change – If your API returns a dynamic ID, don’t assert a fixed value. Use
pm.expect(data.id).to.be.a('number')instead.
Tips
- Use variables – Store values from the response to reuse in later requests:
pm.environment.set("userId", data.id); - Test for schema – Use the
ajvlibrary (included in Postman) to validate the response structure against a JSON schema. Example:const schema = { type: 'object', properties: { id: { type: 'integer' } } }; pm.test('Schema is valid', () => { pm.response.to.have.jsonSchema(schema); }); - Group tests with
pm.testnames – Use descriptive names like “User has email” so failures are clear. - Run tests in a specific order – Use
pm.execution.setNextRequest('RequestName')in the runner to control flow. - Use the Postman Sandbox docs – Postman provides built-in JavaScript libraries like
pm.expect(Chai.js) andpm.response. Refer to the official docs when in doubt.
Troubleshooting
- Tests don’t appear after sending – Make sure you’re in the Tests tab and the script is not empty. Also check that you saved the request.
- Test fails with “Cannot read property ‘name’ of undefined” – The response body might not be what you expect. Log the body first:
console.log(pm.response.text());and check the console (View → Show Postman Console). - Collection runner shows 0 tests – Ensure tests are written in the Tests tab of each request, not in the Pre-request Script tab.
- Newman can’t find the collection – Use the full path to the JSON file, or run from the directory where the file is located.
- Response time test fails intermittently – Network latency can cause flaky tests. Increase the threshold or run multiple times to see if it’s consistent.
Frequently Asked Questions
Q: Can I write tests in languages other than JavaScript?
No, Postman test scripts are JavaScript only. However, you can use external libraries via the require function in the sandbox.
Q: How do I test an endpoint that requires authentication?
Set up the Authorization in the request (e.g., Bearer token). The tests run after the request is sent, so they can verify the response based on the authenticated call.
Q: Can I run tests on a schedule?
Yes, with Postman Monitors. You can create a monitor for a collection and it will run tests periodically from Postman’s cloud.
Q: How do I run a single test from a collection?
In the Collection Runner, you can select specific requests by unchecking others, or use the pm.execution.setNextRequest to skip.
Q: What does pm.expect use under the hood?
It uses the Chai.js assertion library (BDD style). You can use any Chai assertion like to.be.true, to.eql, etc.
Q: Can I test binary responses (e.g., images)?
Yes, you can check the response size or headers, but you can’t parse the body as JSON. Use pm.response.text() to get the raw data.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.