Search code examples
postmanpostman-collection-runner

How to get expected and actual result on console when a test fails in Postman


Am newly trying postman test scripts for testing the API. One thing which i am finding challenging is the expected and actual test result when a test fails. How should I achieve it. I tried with console.log but it wont print if the test case failed. How to achieve a more generic solution with a single function for all the tests.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    console.log("TestCase: Status Code should be 200"+", 
        Expected: "+"Response code should be 200"+", Actual: "+pm.response.code);
});

Solution

  • That assertion error message is auto pushed to the test result section, if the test fails:

    Status code is 200 | AssertionError: expected response to have status code 201 but got 200
    

    You could use this but it would just be repeating what Postman tells you when a test fails:

    pm.test(`Status code is 200 - Actual Status Code: ${pm.response.code}`, () => {
        pm.response.to.have.status(200)
    })
    
    Status code is 200 - Actual Status Code: 404 | AssertionError: expected response to have status code 200 but got 404