pm.expect and pm.response
GET https://postman-echo.com/get?foo1=bar1&foo2=bar2
This request demonstrates how to make assertions with pm.response()
or pm.expect()
within the second parameter of the pm.test()
function.
Send the request to view the results of the sample tests in the Test Results
tab in the response viewer.
Working with the pm.expect()
and pm.response()
functions
The pm.expect()
generic assertion function relies on Chai.js, a BDD / TDD assertion library for node.
pm.test("Environment to be production", function () {
pm.expect(pm.environment.get("env")).to.equal("production");
});
You can also use a second optional parameter with the pm.expect()
function to create a custom error message.
pm.test("Using a custom error message", function () {
pm.expect(false, 'nooo why fail??').to.be.ok;
});
The pm.response()
method uses pm.expect()
under the hood.
Using pm.response()
as your base assertion allows you to receive more specific error messages when debugging. Here are some pm.response()
methods. Review the error messages under the Test Results tab to see the difference.
Request Params
Key | Datatype | Required | Description |
---|---|---|---|
foo1 | string | ||
foo2 | string |
RESPONSES
status: OK
{"args":{"foo1":"bar1","foo2":"bar2"},"headers":{"x-forwarded-proto":"https","host":"postman-echo.com","accept":"*/*","accept-encoding":"gzip, deflate","cache-control":"no-cache","cookie":"sails.sid=s%3AK8csWt5VZ3Oalv85HiBtffDcrSOJ-RiP.V14RjjJqkOv8%2BLXuHRmZ9l50G8S6eU16yLU%2F3N%2F0dKU","postman-token":"c6b9bca5-7369-4da8-a539-6c906d1133f2","user-agent":"PostmanRuntime/7.3.0","x-forwarded-port":"443"},"url":"https://postman-echo.com/get?foo1=bar1\u0026foo2=bar2"}