The best approach to create test cases & folders structure

Originally posted by 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(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!

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 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:

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

@deepak.pathania better now than never :slight_smile:

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

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

Variables

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.

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 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.

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!