# How to use a env var in a test to verify the error message format

**URL:** <https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056>\
**Category:** Help Hub\
**Tags:** tests, variables\
**Created:** [January 11, 2021, 5:59pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056 "2021-01-11T17:59:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![priku18185](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@priku18185](https://community.postman.com/u/priku18185)\
**Post date:** [January 11, 2021, 5:59pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/1 "2021-01-11T17:59:56Z")

</div>

I am creating a new grid using a post request.  
I am storing the name of the grid in the response in an env var as shown below:  
const Grd = pm.response.json();  
postman.setEnvironmentVariable(“grid\_name”, Grd.name);

In my next request I am trying to create a new grid using the same name from the prev request (grid\_name)  
And I have to write a test to validate the following error message displayed:  
“Grid with name ‘Grid vy56xl6xy3n’ exists.”  
where ‘Grid vy56xl6xy3n’ is the grid name from the prev request which is stored in the env var “grid\_name”

My test looks like below where the env var doesn’t get recognized:

pm.test(‘Verify error message for duplicate name’, ()=\>{  
pm.expect(Grd.errors[0]).contains(“Commission grid with name {{grid\_name}} exists.”);  
});

I see the following error for my test:  
Verify error message for duplicate name | AssertionError: expected ‘Grid with name 'Grid vy56xl6xy3n' exists.’ to include ‘Grid with name {{grid\_name}} exists.’

How should I write my test for this scenario?

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 12, 2021, 8:47am UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/2 "2021-01-12T08:47:07Z")

</div>

To access variables in script sections , you should use

```
   pm.environment.get ("grid_name")
```

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [January 12, 2021, 12:51pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/3 "2021-01-12T12:51:08Z")

</div>

@praveendvd is right, but I’d recommend reading the documentation to get a better understanding of how variables work in Postman.

> **[Using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/)**
>
> Using variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![priku18185](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@priku18185](https://community.postman.com/u/priku18185)\
**Post date:** [January 12, 2021, 8:30pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/4 "2021-01-12T20:30:55Z")

</div>

Thanks @praveendvd  
My question here is how to use that env var while validating the error message in my below test

```auto
pm.test(‘Verify error message for duplicate name’, () => {
   pm.expect(Grd.errors[0]).contains(“Commission grid with name {{grid_name}} exists.”);
});

```

I don’t think I can use something like below?

```auto
pm.test(‘Verify error message for duplicate name’, () => {
 pm.expect(Grd.errors[0]).contains(“Commission grid with name pm.environment.get ("grid_name") exists.”);
});

```

---

<div class="post-metadata">

**Author:** ![priku18185](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@priku18185](https://community.postman.com/u/priku18185)\
**Post date:** [January 12, 2021, 8:33pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/5 "2021-01-12T20:33:18Z")

</div>

@allenheltondev Sure! Thanks.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 12, 2021, 8:50pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/6 "2021-01-12T20:50:49Z")

</div>

You’d need to do something like this in order to use the variable like that:

```auto
pm.test(‘Verify error message for duplicate name’, () => {
 pm.expect(Grd.errors[0]).contains(`Commission grid with name ${pm.environment.get("grid_name")} exists.`);
});

```

---

<div class="post-metadata">

**Author:** ![priku18185](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@priku18185](https://community.postman.com/u/priku18185)\
**Post date:** [January 12, 2021, 11:46pm UTC](https://community.postman.com/t/how-to-use-a-env-var-in-a-test-to-verify-the-error-message-format/19056/7 "2021-01-12T23:46:41Z")

</div>

That works. Thanks @danny-dainton
