1

I'm writing some tests in Postman. When writing the error message, I must input manually the name of the variable that is being tested. Is there a way to avoid having to put it by hand? In some tests, there are 200 variables. If something like "pm.response.parameter" existed it would be of great help as you could write a generic error message and just have to paste it in all messages.

At the moment i am using something like:

pm.expect(value.proctrEs).to.eql(null,"error en proctrEs : "+pm.response.code);

But I was wondering if there is something like:

pm.expect(value.proctrEs).to.eql(null,"error en "+ pm.response.parameter + " " + pm.response.code);

enter image description here

bibu3344
  • 163
  • 2
  • 10
  • There are a lot of keys in response json, how do you know which key will be put into error message? – lucas-nguyen-17 Jan 13 '22 at 15:13
  • i dont need the keys from the response. i need the parameter of this `expect` method. Actually, what I want is that the error message show me the information of which property is the one that has failed. So any solution that leads to that is fine for me. – bibu3344 Jan 13 '22 at 15:18
  • I get it. you need to write a function to do that. There is no such a thing like `pm.response.parameter`. – lucas-nguyen-17 Jan 14 '22 at 02:20

1 Answers1

1

This function will be like this:

function checkProperty(path, value) {
    let property = path.split(".").at(-1);
    pm.expect(eval(path)).to.eql(value,`error en ${property}: ${pm.response.code}`);
}

Example:

const res = pm.response.json();

pm.test("check response", () => {
    checkProperty("res.headers.host", "postman-echo.com1");
    checkProperty("res.url", "http://postman-echo.com/get1");
})

Tested with http://postman-echo.com/get

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20