For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Postman
PricingEnterprise
Contact SalesSign InSign Up for Free
HomeDocs
HomeDocs
      • Overview
        • Overview
        • Write pre-request scripts
        • Write tests
        • Script examples
        • Dynamic variables
          • Overview
          • pm variables methods
          • pm.vault
          • pm.cookies
          • pm.request
          • pm.response
          • pm.sendrequest
          • pm.visualizer
          • pm.test and pm.expect
          • pm.require
          • pm.execution
          • pm.message
          • pm.info
          • pm.mock
          • pm.datasets
          • pm.state
        • Troubleshoot test errors
Postman API Platform

Product

  • Postman Overview
  • Enterprise
  • Spec Hub
  • Flows
  • Agent Mode
  • API Catalog
  • Fern
  • Postman CLI
  • Integrations
  • Workspaces
  • Plans and pricing

API Network

  • App Security
  • Artificial Intelligence
  • Communication
  • Data Analytics
  • Database
  • Developer Productivity
  • DevOps
  • Ecommerce
  • eSignature
  • Financial Services
  • Payments
  • Travel

Resources

  • Postman Docs
  • Academy
  • Community
  • Templates
  • Intergalactic
  • Videos
  • MCP Servers

Legal and Security

  • Legal Terms Hub
  • Terms of Service
  • Postman Product Terms
  • Security
  • Website Terms of Use

Company

  • About
  • Careers and culture
  • Contact us
  • Partner program
  • Customer stories
  • Student programs
  • Press and media
Twitter iconLinkedIn iconGithub iconYouTube iconInstagram iconDiscord icon
Download Postman
Privacy Policy

© 2026 Postman, Inc.

On this page
  • pm.test
  • pm.expect
Tests and scriptsWrite scriptsPostman sandbox reference

Writing tests and assertions in scripts

||View as Markdown|
Was this page helpful?
Previous

Script Postman visualizations

Next

Import packages into your scripts

Built with

You can add test specifications and assertions to your scripts using the pm.test and pm.expect methods.

pm.test

Use the pm.test method to add test specifications inside Pre-request or Post-response scripts (for HTTP) or Before invoke, On message, or After response scripts (for gRPC). Tests include a name and assertion function.

A test for HTTP request would look something like the following:

1pm.test(testName , specFunction)

Where:

  • testName - A string that contains the test’s name.
  • specFunction - The function defining the test logic.

Postman outputs test results as part of the response. The pm.test method returns the pm object, enabling you to chain calls.

Examples

Check whether a response is valid to proceed:

1pm.test("response should be okay to process", function () {
2 pm.response.to.not.be.error;
3 pm.response.to.have.jsonBody('data') // checks existence of a property in the JSON response
4 pm.response.to.have.jsonBody('data', { "id" : 1 }); // Performs deep comparison
5});

Test an asynchronous function using an optional done callback:

1pm.test('async test', function (done) {
2 setTimeout(() => {
3 pm.expect(pm.response.code).to.equal(200);
4 done();
5 }, 1500);
6});

Get the total number of tests run from a specific location in code:

1pm.test.index(); // Number

Include multiple assertions to group the related assertions in a single gRPC test:

1pm.test("Should receive update events for both users", function () {
2 pm.response.messages.to.include({ action: 'update', userId: 'user1' });
3 pm.response.messages.to.include({ action: 'update', userId: 'user2' });
4});

Get the total number of tests run from a specific location in code:

1pm.test.index; () =>number

Skip a test:

1pm.test.skip: (testName, specFunction) => pm

pm.expect

The pm.expect method enables you to write assertions on your response data using ChaiJS expect BDD syntax.

1pm.expect(value: *): Assertion
  • value - What you want to test, where * can be any type of value, such as a string or integer.
  • Assertion - A Chai Assertion object containing chainable methods.

You can also use pm.response.to.have.* and pm.response.to.be.* to build your assertions.

See Postman test script examples for more assertions.

Examples

Check if the response returns an HTTP 200 OK response:

1pm.test("Response status code is 200", function () {
2 // value: pm.response.code (a number)
3 // Assertion: checks if it equals 200
4 pm.expect(pm.response.code).to.eql(200);
5});

Check if the request’s response body contains the true value:

1pm.test("Response body has success = true", function () {
2 const jsonData = pm.response.json();
3 // value: jsonData.success (a boolean)
4 // Assertion: checks if it equals true
5 pm.expect(jsonData.success).to.eql(true);
6});