pm.expect and pm.response
GET https://postman-echo.com/get?foo1=bar1&foo2=bar2
Within the second parameter of the pm.test()
function, you can use either pm.response()
or pm.expect()
to make your assertions.
Under the Tests tab in the Qodex app, review the tests comparing and contrasting the 2 methods.
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 2nd 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"}