You can add test specifications and assertions to your scripts using the pm.test and pm.expect methods.
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:
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.
Check whether a response is valid to proceed:
Test an asynchronous function using an optional done callback:
Get the total number of tests run from a specific location in code:
Include multiple assertions to group the related assertions in a single gRPC test:
Get the total number of tests run from a specific location in code:
Skip a test:
The pm.expect method enables you to write assertions on your response data using ChaiJS expect BDD syntax.
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.
Check if the response returns an HTTP 200 OK response:
Check if the request’s response body contains the true value: