Writing Tests in Postman with JavaScript

Postman is more than just an API client—it’s a full-fledged testing framework for validating API responses. One of its most powerful features is the ability to write JavaScript-based tests directly within the interface, allowing developers and QA professionals to automate validation and improve API reliability.

In this article, we will explore how to write tests in Postman using JavaScript, from basic status code checks to complex data validations.

Why Write Tests in Postman?

Automated testing is critical in modern API development. Writing tests in Postman allows you to:

  • Ensure API endpoints work as expected
  • Validate response status, headers, and body
  • Automate regression testing
  • Support continuous integration workflows

These tests can be run manually through the Postman interface or automatically through the Collection Runner or Newman.

Where to Write Tests in Postman

  1. Open any API request in Postman.
  2. Click on the “Tests” tab (located next to “Params”, “Headers”, etc.).
  3. Write JavaScript code in the editor area to create test scripts.

Postman exposes the pm object to help access request and response details.

Basic Example: Status Code Check

javascriptCopyEditpm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

This script checks if the response returned a 200 OK status.

Checking Response Body Content

javascriptCopyEditpm.test("Response contains userId", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property("userId");
});

This validates that the JSON response has a property called userId.

Validating Response Time

javascriptCopyEditpm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

This ensures the response is quick enough for performance requirements.

Header Validation

javascriptCopyEditpm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

Use this to confirm the server is returning the correct content type.

Writing Multiple Tests Together

javascriptCopyEditpm.test("All validations", function () {
    const jsonData = pm.response.json();
    pm.response.to.have.status(200);
    pm.expect(jsonData).to.have.property("id");
    pm.expect(jsonData.userId).to.equal(1);
});

You can combine multiple assertions in one test block or separate them for better readability.

Using Environment and Global Variables

javascriptCopyEditpm.test("Save token to environment", function () {
    const jsonData = pm.response.json();
    pm.environment.set("authToken", jsonData.token);
});

This stores a value from the response into an environment variable, useful for chained requests.

Common Built-in Methods

  • pm.response.code: Access response status code
  • pm.response.text(): Get raw response
  • pm.response.json(): Parse response as JSON
  • pm.environment.set(): Store variables
  • pm.expect(): Make assertions (powered by ChaiJS BDD)

Conclusion

Writing tests in Postman with JavaScript empowers developers and testers to automate validations, catch bugs early, and ensure robust API functionality. With Postman’s scripting capabilities, you can go beyond manual testing and build scalable test suites that support rapid development and continuous integration.

YOU MAY BE INTERESTED IN

The Art of Software Testing: Beyond the Basics

Automation testing course in Pune

Automation testing in selenium

Mastering Software Testing: A Comprehensive Syllabus

Scroll to Top