Using response body in test result

Hello,

I would like to see some of my response body values in test result. Example response:

{
    "Id": 10861,
    "Name": "RC_API_45",
    "TypeId": 1
}

I can parse ID in Tests tab:

var jsonData = JSON.parse(responseBody);
pm.environment.set("CustomerId", jsonData["Id"]);

Now I`d like to use that value in Test Results log, I already have confirmation of the status code:

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

And below that I`d like to have sth like this:

“Customer ID is ____”

How can I make just extracted variable to be visible in TestsResults?

Hello @Gabber
You can add one more test scenario where you validate if there is the “id” property and in the test name you concatenate with the answer id.

I’ll put an example below:

pm.test("Customer ID is "+jsonData.id, function() {

    //Validates whether the "id" property exists within the response json
    pm.expect(jsonData).to.have.property('id');

});

Hope this helps :slightly_smiling_face:

1 Like

Yes! That was exactly what I was looking for! Thank you @cmnazario !

1 Like