# Use one only instance of tests and re-use

**URL:** <https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626>\
**Category:** Help Hub\
**Tags:** tests, testscripts, testing\
**Created:** [January 8, 2018, 10:44am UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626 "2018-01-08T10:44:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eloyucu](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@eloyucu](https://community.postman.com/u/eloyucu)\
**Post date:** [January 8, 2018, 10:44am UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626/1 "2018-01-08T10:44:40Z")

</div>

Hello everyone.

I have some folder with different request, something like:

```
Folder 1
     request 1
     request 2
     request 3
Folder 2
     request 1
     request 2
     request 3
Folder 3
     request 1
     request 2
     request 3

```

The thing is that all _request 1_ use the same test (same for _request 2_ and _request 3_). The reason for this is that there are some little differences on the bodies, but the structure of the responses is similar.

The problem here is that I need to duplicate all those tests for all the folders, and if I make some change replicate it in all the instances of the tests.

Isn´t there any way to write this code only once, encapsulate like functions and then call these functions from the Test area of the request??

Thank you very much.

---

<div class="post-metadata">

**Author:** ![dhoyt](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/dhoyt/32/238_2.png) [@dhoyt](https://community.postman.com/u/dhoyt)\
**Post date:** [January 8, 2018, 9:28pm UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626/2 "2018-01-08T21:28:21Z")

</div>

You can set a global variable that contains a “function” in the “pre-request” script of request 1.

As an example, this is some code that I run in the “test” script for a lot of the endpoints that I run.

```javascript
pm.globals.set('test200', test200 = function () { 
  if( responseCode.code != 200 ) {
    tests['expected responseCode is 200 ... responseCode received = ' + responseCode.code] = responseCode.code = false ;
    tests['responseCode.name = ' + responseCode.name] = true;
    tests['responseCode.detail = ' + responseCode.detail] = true;
    }
};)

```

Once it has been loaded once into the global variable pool, the test200 function can now be called from any of your “requests” in the pre-request or test scripts.

```javascript
eval( pm.globals.get('test200') );
test200();

```

Or, you can load “test200” into the “global variables” file (globals.postman\_globals.json) that will be automatically loaded when each collection is run and you can skip the “pm.globals.set”.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![eloyucu](https://avatars.discourse-cdn.com/v4/letter/e/e9c0ed/32.png) [@eloyucu](https://community.postman.com/u/eloyucu)\
**Post date:** [January 9, 2018, 12:36pm UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626/3 "2018-01-09T12:36:45Z")

</div>

Thanks dhoyt!!

I will try (when I have a little of time 😕 ).

---

<div class="post-metadata">

**Author:** ![tophsic](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@tophsic](https://community.postman.com/u/tophsic)\
**Post date:** [March 14, 2019, 3:29pm UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626/4 "2019-03-14T15:29:30Z")

</div>

I would prefer to use the same request which depends on environment variables instead of duplicate requests.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [December 9, 2024, 2:33pm UTC](https://community.postman.com/t/use-one-only-instance-of-tests-and-re-use/626/5 "2024-12-09T14:33:42Z")

</div>

Closed on December 9, 2024, 2:33pm UTC

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