I have environment variables defined and want to use them in Tests to confirm data sent out is what comes back in

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
How do I define an already existing environment variable which has been populated with values for the post to run in tests to confirm that that is the data that comes back in the response

Details (like screenshots):
I have pre-populated environment variables to be used as values in the Posts and this is working fine.

I need then to test that the existing environment variables that I have defined are what come back in the response, The Post is working fine, it sends the data from the environment variables and creates the entry, but I cannot use the existing environment variable in the tests to compare the environment variables against the response, I can do it if I hard code the expected response in that works. I have tried pm.environment.get in pre-request script, but I cannot do pm-environment.set as it resets the variable which is not what I want?

Example of Post

Example of Tests

How I found the problem:
I am learning, so most likely missing something,

I’ve already tried:

I have tried searching and what I find relates creating an environment variable from the response and using that, I do not want to do that I want to verify that existing values in the existing variable are what come back in the response.

Hi @pabloh1 - try using get() to reference the variable instead of set() to define the variable. Under the Tests tab, Postman requires JavaScript - like pm.environment.get("name").

In the example of your request body, you can use double curly braces like {{name}} to reference a variable since that is a text editor and Postman will use string substitution to resolve variables.

Hi Joyce, thanks but the request body works fine with the curly braces I send the data from the environment variable, what I cannot do is in the test refer to the same existing environment variable and use it to check what was sent out was what come back in the response body,

i.e. My {{name}} environment variable is set to James Dunken, I use this to post and the response body comes back with the name James Dunken as expected. I am then writing a test to check that what is in the environment variable that I sent out comes back in, but I cannot get the pm.expect to recognise the {{name}} as an environment variable.

When I type the name in the pm.expect, it resolves to any abc name, not a environment variable

pm.test (“Check if name is correct”, function () {

var jsonData = pm.response.json();

pm.environment.get(“name”) correctly resolves name as James Dunken

pm.expect(jsonData[0].name).to.eql("{{name}}");**Does not work, I cannot get it to be the existing environment variable (already used in Body) and resolve this to James Dunken so when it checks the Response it is looking for James Dunken,  The reason I am doing this is so that I can pre-populate environment variables with data for a specific user and run tests against that and then run the same tests for a different user and confirm the results, i.e. they do not have that specific item available to them, for all the tests and use throughout as I need to verify what is coming back is what was expected,  I do not want to capture the response as a variable, I want to use an existing environment variable value to check without needing to type it manually in each test which works fine **

Any Thoughts, what am I missing, is pm-expect not the correct check?

Hi Since the initial response from Joyce, I have resolved the issue.

pm.test (“Check if name is correct”, function() {
var jsonData = pm.response.json();

the other option before this worked if value was not json was to search for text.


both of these worked, hope it helps anyone who has the same issue