Is It able to run difference requests with different data files in one Collection?

We have a lot of API in different micro-services , and we are trying to put all request test cases in one collection for one micro-service API. then every time, each micro-service has new release, it will automatically run test cases in that one collection;

But It seems like Postman can only run one collection with one data file and set iterations for the whole collection;
In our case, different request has different data file;
Is it able to set up data file and iteration for each request in the same collection?

Thanks!

3 Likes

I have same requirement. Is it possible then how to do it. Please guide.!

Hey @kgkamble, would you mind explaining a little bit more on your use case please? I’m sure we can come up with a way to do what you want.

I’m just having a hard time fully understanding what you are trying to do.

@allenheltondev

Thanks for the reply.

PFB the test scenario:

I have one collection which has 4-5 API requests in which I have to pass a few parameters like username, password, or access token, a value in JSON format by using data files. I have already created the required environmental variables, data files. For each request, I have created an independent CSV file. By using a single request and single data file, it works as expected in Postman as well as in Newman.

Now next step is I have to run the entire collection which has an independent data file (CSV) for each request by using Newman. I know by using Postman, we can’t execute this scenario.

Please advise on this. Please reply if you are still struggling to understand the cases.

2 Likes

Why do you need them to be in separate csv files? I don’t think you can do what you want.

Thinking about it from the collection runner/newman’s standpoint, it doesn’t make sense to drive requests off of different data sources in the same collection.

If you combined them all together, it would be able to iterate properly.

Hello, sorry for bringing up the old thread, but it seems totally valid to me as well - as I’m experiencing the same issue.
It might be because of my lack of experience with Postman, of course - but logically, I’d expect a feature to use/attach different data sets (data files in this case) for different requests inside one collection.
It looks obvious to me that different requests (like Get/Update/List/Create/Delete, etc.) would have different request parameters.
So far, I was able to make it work only by putting all the different parameters into one data file’s array element - but it doesn’t look the best/intuitive way of handling it.

1 Like

Hi @allenheltondev,

Can you please elaborate on how to have a single data file for all the API requests (with different parameters) in a single collection?

Thank You

Three years ago me might have known something different than I do now, but what I imagine I meant is conditionally branching a collection based on the properties of the iteration. Meaning:

  • If the current iteration has property A, use postman.setNextRequest(ARoute)
  • If the current iteration has property B, use postman.setNextRequest(BRoute)

But again, not sure if that’s what I meant all those years ago. Would the above work for you? I worry that it’s not very maintainable.

1 Like

@allenheltondev it does make sense to have different files. In my scenario.

  1. Request A doesn’t need a data file. It can run as is. Creating a new project.
  2. Request B needs a data file. Im creating our standard labels for the project. But you can only create one label at a time so we need the data file.
  3. So on and so on.

But the problem is the collection runner is applying that data file to every request it runs so its trying to create a project for every entry in the data file. And I don’t see how combining all of the data files the OP or @kgkamble have into one will resolve the problem, if the Runner is going to loop every request in the collection against the data file.

I’d love to be wrong. If there’s a solution it would make life better. Help me Obi Won…

So request A) needs to run once, and request B) needs to loop.

You haven’t said what request 3 needs to do. So I can’t comment on that.

I can think of two ways of doing this.

Option 1.

Add a column to the data file tell it which requests it should run against.

Then have a pre-request script that reads that entry from the data file and use the skip request feature if the name isn’t on the list.

In the first iteration, request A) will be in the list. In the requests for the labels, then it won’t be and will therefore be skipped each time.

Option 2.

Use a JSON data file and structure the data file, so it only has a single object at the top of the structure, but then has an array of the labels nested in that object.

For example.

{
    "testData": {
        "labels": ["label1", "label2", "label3"]
    }
}

This means it will only run one iteration and that iteration has a single object in it, which will be an array.

You can then use that array with the array.shift() and setNextRequest method to create the loop for request B.

You need to add something like the following to the pre-request script for request B.

if (typeof array === 'undefined' || array.length == 0) {
    // get array from iterationData variable which should contain
    // the current iteration data
    array = pm.iterationData.testData.labels
}

let currentLabel = array.shift();
pm.collectionVariables.set("label", currentLabel);

if (array.length > 0) {
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 
2 Likes

That’s great. Thank you, Mike. I’ve never stumbled across the skipRequest() function and the ability to use it against a runner. :fist_right: :fist_left: