Set error message

How can I use custom message if test fails? For example, I have test:

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

And I want see in results message about another status code in case of test failing (“Status code is 404”, “Status code is 500”, etc.)

Here is a very simple example …

if(responseCode.code != 200) { 
    tests["TEST FAILED - Expected responseCode is 200 ... responseCode received = " + responseCode.code] = responseCode.code === 200;
}
else {
    tests["TEST PASSED - responseCode is 200 ... responseCode received =" + responseCode.code] = responseCode.code === 200; 
}

You can also use the pm.expect along with .equal which accepts a second argument with custom message.

For example:

pm.test('Status code', function() {
    pm.expect(pm.response.code).equal(200,'Status received is ' + pm.response.code);
});
1 Like

This might be more inline with what he wants …

pm.test('Status received is ' + pm.response.code, function() {
    pm.expect(pm.response.code).equal(200);
});

This will always display the response code regardless if it is 200 or not. The “expect” works perfectly determining if its a PASS (green) or FAIL (red) in the output.

1 Like