How to build flexible/reused tests in collection

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

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”

     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);
         });
    

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

@Alextunchii This can be done in an easier manner by using pm.sendRequest 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 :smile:

@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

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