How to get assertion result in variable using Postman Test

321 views Asked by At

I am unable to get the result of the assertion. Below mentioned are the example

//Code

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

I wanted to get the result in variable so i can use this assertion value in other code(as mentioned in attached email).

For example: Save value of "Status code is 200 | AssertionError: expected response to have status code 2001 but got 401" in VARIABLE

See image

1

There are 1 answers

0
PDHide On
let error;
pm.test("Status code is 200", function () {
    try {
        pm.response.to.have.status(2001);
    }
    catch (err) {
        error = err
        pm.response.to.have.status(2001);
    }

});



console.log(error)

just use try and catch, I am not sure why this is a use case. you can this to variable as

 pm.environment.set("error",error)