# The best approach to create test cases & folders structure

**URL:** <https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631>\
**Category:** Help Hub\
**Created:** [May 16, 2018, 12:40pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631 "2018-05-16T12:40:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [May 16, 2018, 12:40pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/1 "2018-05-16T12:40:47Z")

</div>

Originally posted by [Alextunchii](https://github.com/Alextunchii)

> <https://github.com/postmanlabs/postman-app-support/issues/4561>
>
> Hi, sorry if this question is redundant here, it is, I will risk on stackoverflo…w, 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(\*\*Folder with test in Schools folder\*\*)
> \_where requests:\_ 
> 1. POST: Create a list
> \`\`\`
> var jList = JSON.parse(responseBody);
> postman.setEnvironmentVariable("list\_id", jList.data.id);
> \`\`\`
> 2. POST: Add some filters there
> 3. GET: lists/{{list\_id}} --\>whete in "Test" code:
> \`\`\`
> var allLists = JSON.parse(responseBody);
> 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);
> });
> \`\`\`
> Where I take and compare the number that came out in in previous POST request
> 4. DELETE: delete this list
> 
> 
> The small problem is:
> for every (\*\*Folder with test\*\*) I send POST request to create a list
> for every GET request, I parse body and compare the different number
> I want to:
> If possible in precondition in one place create a list for every folder/test
> in one place as postcondition delete this list
> If it possible somehow make better:
> \`\`\`
> 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);
> });
> \`\`\`
> in order not to write in in every GET request in "Tests" it also would be greate.
> 
> Is it possible to bring it out to collection or folder variables/environments? to make it more delicate. 
> I uploaded this collection with structure if my text is not clear enough:
> \[test.zip\](https://github.com/postmanlabs/postman-app-support/files/2007943/test.zip)
> 
> Thanks!

* * *

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(Folder with test in Schools folder)  
where requests:

POST: Create a list  
var jList = JSON.parse(responseBody);  
postman.setEnvironmentVariable(“list\_id”, jList.data.id);  
POST: Add some filters there  
GET: lists/{{list\_id}} --\>whete in “Test” code:  
var allLists = JSON.parse(responseBody);  
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);  
});  
Where I take and compare the number that came out in in previous POST request  
4. DELETE: delete this list

The small problem is:  
for every (Folder with test) I send POST request to create a list  
for every GET request, I parse body and compare the different number  
I want to:  
If possible in precondition in one place create a list for every folder/test  
in one place as postcondition delete this list  
If it possible somehow make better:

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);  
});  
in order not to write in in every GET request in “Tests” it also would be greate.

Is it possible to bring it out to collection or folder variables/environments? to make it more delicate.  
I uploaded this collection with structure if my text is not clear enough:  
test.zip

Thanks!

---

<div class="post-metadata">

**Author:** ![deepak.pathania](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deepak.pathania](https://community.postman.com/u/deepak.pathania)\
**Post date:** [July 6, 2018, 6:29am UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/2 "2018-07-06T06:29:30Z")

</div>

Hey @Alextunchii, sorry for the delay in response, this might have been missed by mistake.

If I understand your question correctly, you would like to emulate the functionality of `beforeEach` and `afterEach` in your tests for setup and teardown purposes so that you don’t have to do it in each individual request.

If that is indeed the case, then you can use [collection and folder level pre request scripts and tests](http://blog.getpostman.com/2017/12/13/keep-it-dry-with-collection-and-folder-elements/) to achieve the same which execute before every request in the collection and folder respectively.

So in your case, you could create all the lists once in the folder level pre request script and delete them in the test scripts. You can abstract out individual tests by encapsulating them in a higher level entity and do all the setup and teardown at this new entity level, where entity can be a folder or collection.

For writing collection and folder level scripts, simply click the ellipsis `...` icon in front of every collection and folder in the side bar and click on edit and go to the `Pre-request scripts` tab. This is how it looks for your reference:

 ![21%20PM](https://us1.discourse-cdn.com/getpostman/original/3X/8/3/83ab18f9888d22d526c64348800258d10566236a.png)

Feel free to reach out if this was not your query or if you need additional details. Cheers.

---

<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:47pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/3 "2018-07-07T14:47:57Z")

</div>

@deepak.pathania better now than never 🙂

Year, you got me right! I will research your advice and will write about the results in case something made successfully or not

---

<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, 3:27pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/4 "2018-07-07T15:27:16Z")

</div>

Was thinking how to do this, but can’t figure out, perhaps here exists some simple thing that I don’t see.  
Will try to explain in much clearer way to you in order you were able to give me accurate answer if possible.

Structure

 ![image](https://us1.discourse-cdn.com/getpostman/original/1X/be85806ee4b36f2a4fffcedced62505b8cc5bc07.png)

---

<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, 3:28pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/5 "2018-07-07T15:28:01Z")

</div>

Variables

 ![variables](https://us1.discourse-cdn.com/getpostman/original/3X/2/b/2b6ac63bc815b9408b40fe7c762652f39da51961.jpeg)

---

<div class="post-metadata">

**Author:** ![deepak.pathania](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deepak.pathania](https://community.postman.com/u/deepak.pathania)\
**Post date:** [July 9, 2018, 7:21am UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/6 "2018-07-09T07:21:53Z")

</div>

Hey @Alextunchii, thanks for the detailed problem description.

What you’re basically asking for is a way to run a request before and after all the requests in a particular folder without having those requests in the folder itself.

This is currently not possible directly through the interface.

However, there is a workaround you can use _provided that your request names are unique_ in the folder using folder level scripts. Please note that this would break if you have multiple requests with the same name.

So basically in your test folder, you could have a **folder level script behind a conditional block** to send a request to the `Create a list` endpoint. The folder level script executed before each request but if you have a unique name for the first request, you can use that for conditional execution.

Here’s how it would look.

```javascript
if (pm.info.requestName === 'Add 1 entity to list') {
  pm.sendRequest(`Create list endpoint`, function(err, res) {
    // do the response parsing and other stuff here.
  })
}

```

Similarly for the last request, if the name is unique, you can add a folder level test script that uses `pm.sendRequest` to send the delete list call behind the conditional for the name of the last request. You can refer to some of the examples of [pm.sendRequest](https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990) here.

Additionally, if the names of all the requests are same in all your folders, you can add all the folders in a base folder, and add the script to that base folder’s scripts so that you don’t even have to do this in every folder’s script level.

I hope this helps, in the future, we are thinking of coming up with execution schemas that would make all the ordering much easier, I cannot give you an ETA on that but I would try and update this thread when it is out.

Feel free to reach out with additional queries.

---

<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 16, 2018, 7:00pm UTC](https://community.postman.com/t/the-best-approach-to-create-test-cases-folders-structure/1631/7 "2018-07-16T19:00:32Z")

</div>

Hello! Sorry, could answer some time! Thank you for you attention and the answer, I will try to implement it!

Thank you one more time for the support!
