How to use variables in pm.test name?

Hello,

I’m currently working on some API tests and in general it goes well but at this point I want to add some additional information stored in variables to test name/messages. I run requests in collection mode.

How to use (i.e. global) variables im pm.test name?

I have test like this:

var response = JSON.parse(responseBody)
postman.setGlobalVariable(“toolkit_id”, response.ToolkitId);

pm.test(“Toolkit successfully created.”, function () {
pm.response.to.have.status(200);
});

and I want to use it like this:

var response = JSON.parse(responseBody)
postman.setGlobalVariable(“toolkit_id”, response.ToolkitId);

pm.test(“Toolkit successfully created with ID: {{toolkit_id}}.”, function () {
pm.response.to.have.status(200);
});

I’ve already read topics like this or this but I still can’t make it works as I want. I will really appreciate any help with my problem.

Hello, you need to write code like this:

pm.test(`Toolkit successfully created with ID: ${pm.environment.get('toolkit_id)}`, function () {
    pm.response.to.have.status(200);
});

You need include name of the test in brakets like this - `` and variable you need in brakets whith $ symbol like this - “${name of the variable}”.

Thank you for your help, @PaulKP! :saluting_face:

At first I get the syntax error because " ’ " missing but overall it works just as I want.

pm.test(Toolkit successfully created with ID: ${pm.environment.get('toolkit_id')}, function () {
pm.response.to.have.status(200);
});

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.