# How to build flexible/reused tests in collection

**URL:** <https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679>\
**Category:** Help Hub\
**Tags:** tests, collections\
**Created:** [May 23, 2018, 9:16am UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679 "2018-05-23T09:16:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alextunchii](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Alextunchii](https://community.postman.com/u/Alextunchii)\
**Post date:** [May 23, 2018, 9:16am UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/1 "2018-05-23T09:16:49Z")

</div>

Hi!  
Trying to make flexible test cases/reusable.  
I have this structure:  
Collection:  
Folder: where login and getting token env. which then I pass to each next endpoint  
==Folder: as a name for Categories(folders with tests)  
====Folder(attached inside) with calls  
====Folder(attached inside) with calls  
====Folder(attached inside) with calls

(this structure above - folder with categories can be duplicated and rising wider, now it more than 20 of"Folder(attached inside) with calls"

In every ====**Folder(attached inside) with calls**  
I have these endpoints inside:  
**POST:** create a list  
**POST:** add filters  
**GET:** get information with a count on this list  
**DELETE:** delete this list

 ![1527066498243](https://us1.discourse-cdn.com/getpostman/original/3X/a/7/a7a8db3e16de38043da8bf54901e6fcfa0b04a88.jpeg)

In **POST** create list I parse body on these parameters  
var jList = JSON.parse(responseBody);

```
    postman.setEnvironmentVariable("list_id", jList.data.id);
    postman.setEnvironmentVariable("segment_id1", jList.data.segments[0].id);
    postman.setEnvironmentVariable("targeting_crit_id1", jList.data.segments[0].targetingCriteria[0].segmentCriteriaId);

```

list\_id - pass to DELETE list and GET count  
segment\_id1, targeting\_crit\_id1 - pass to POST add filters

In **GET:** get information with a count on this list - I have this test:

```
pm.test("test count", function () {
    const value = allLists.data.count;
    pm.expect(typeof value === 'number').to.eql(true);
    pm.expect(value > 0 && value < 999999).to.eql(true);
});

```

Questions:

1. Can I get POST: create a list endpoint brought out as precondition for every test scenario in order not to put Create and Delete a list in every folder (stupid copy-paste)

2. Can I get this test or just **value \> 0 && value \< 999999** (number range: 0 - 999999) added as separate global variable or environment for every different test, something like that:

3. geo: value \> 0 && value \< 999999

4. job: value \> 0 && value \< 30000

5. inst: value \> 0 && value \< 40444 - in order not to hardcode the different number in every endpoint “tests”

Please, give an advice, is there any better practise?

---

<div class="post-metadata">

**Author:** ![kunagpal](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kunagpal/32/9_2.png) [@kunagpal](https://community.postman.com/u/kunagpal)\
**Post date:** [July 5, 2018, 8:59pm UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/2 "2018-07-05T20:59:10Z")

</div>

@Alextunchii This can be done in an easier manner by using [pm.sendRequest](https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference#pmsendrequest) and collection level scripts, which have been documented here: [https://www.getpostman.com/docs/v6/postman/scripts/test\_scripts#adding-a-test-script-to-a-collection-or-folder](https://www.getpostman.com/docs/v6/postman/scripts/test_scripts#adding-a-test-script-to-a-collection-or-folder) 😄

---

<div class="post-metadata">

**Author:** ![Alextunchii](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Alextunchii](https://community.postman.com/u/Alextunchii)\
**Post date:** [July 7, 2018, 2:45pm UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/3 "2018-07-07T14:45:42Z")

</div>

@kunagpal thanks for replying. I know about this feature, but it does not fit in my case I think, because I have also delete request and I don’t need any request execution after or before it

My needs are revealed here already, I will research once again and will try to do like that  
Actually as told in that topic I need a BeforeAll after AfterAll conditions something like that

> [@The best approach to create test cases & folders structure](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631):
>
> Originally posted by [Alextunchii](https://github.com/Alextunchii) Hi, sorry if this question is redundant here, it is, I will risk on stackoverflow, but I thought it is the best place to ask it. I have been using Postman so much and built a lot of useful thing in it, but now need to implement one more thing. Briefly: Need to create test cases to test a correct records counting for every institution. Now I handled it like that —Collection –Folders -Districts(folder) -Colleges(folder) -Schools(folder) -\*Principal(Fo…

---

<div class="post-metadata">

**Author:** ![Alextunchii](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@Alextunchii](https://community.postman.com/u/Alextunchii)\
**Post date:** [July 7, 2018, 2:50pm UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/4 "2018-07-07T14:50:07Z")

</div>

sorry, actually forgot to say, you are also right 🙂 with the suggested option

---

<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:** [September 6, 2023, 1:11pm UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/5 "2023-09-06T13:11:29Z")

</div>

Split this topic on September 6, 2023, 1:11pm UTC

A post was split to a new topic: [[Video] Reuse test cases and scripts in Postman](https://community.postman.com/t/video-reuse-test-cases-and-scripts-in-postman/51446)

---

<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:** [September 6, 2023, 1:11pm UTC](https://community.postman.com/t/how-to-build-flexible-reused-tests-in-collection/1679/6 "2023-09-06T13:11:51Z")

</div>

Closed on September 6, 2023, 1:11pm UTC


