Iterating only a single request inside a collection with test data specified in a csv file

I have a requirement where in there are 5 requests inside the collection.
I have a csv file where list of 100 test data rows are stored.
I want the first 4 request in the collection to be executed only once.
Whereas want the 5th request to be iterated 100 times as per the test data csv file…

I tried following but did not work…
I thought of keeping a collection level counter and then in the pre-request script of the first request…I tried putting a condition that if the counter is 0 then execute this request else set the postman.setNextRequest(“Fifth Request”)…
But, this did not work as setNextRequest is not allowed in the Pre-request script as per my understanding…

I cant think of any other solution…Any help will be really appreciated.
Thanks in advance,
Nishant Shah

You are on the right track. I would try it the opposite way you are suggesting.

Implement the counter in your collection variables like you said, but in the tests section of your fourth request, check the counter and use pm.setNextRequest('First Request') for your first 4 iterations.

You’re also going to need to set a flag to make sure your first 4 requests don’t run again on subsequent iterations. So here is what I would do:

  1. Add a bootstrapCollection variable in your collection
  2. Add a new request at the very beginning of your collection, to the postman echo endpoint
  3. In the tests of the postman echo request, check to see if the bootstrapCollection is true/false.
    a. If it is true, don’t do anything
    b. If it is false, use pm.setNextRequest('Fifth Request') so you skip over all the setup requests.
  4. Make sure you set bootstrapCollection to false in the last request of your setup requests

That should do what you want. Let me know if you need any clarification.

Cool, thanks @allenheltondev for the quick answer.
In fact, few minutes back I ended up doing the exact same solution you suggested :slight_smile:
At the time of implementing this solution I was feeling a bit guilty as it felt a hacky solution to me…as had to introduce the postman test request at the top…
But, now that you also suggest the same solution, I am feeling a bit better :slight_smile:

I use those tests everywhere in my collections. Postman echo is great for resolving dynamic variables that you want to use in multiple requests.

I’ll hit the echo to resolve variables and story them off
Then I’ll post to add a new resource
Then I will get the added resource in another request and verify the contents against the variables from step 1

1 Like

Hi @allenheltondev and @nishant.shah2 ,

I am also facing the same issue. Can you please explain in detail (maybe with help of sample scripts) how I can run the first 4 tests only once and some specific tests with iteration-data from an external JSON file?

Thank you so much in advance!

Best,
Vaibhav Sharma

Hi @vaibhav.sh.05 ,
Basically, the logic here is to find out a way such that you can compare a flag and on the basis of its value jump to a specific request (fifth request in our case) OR in other words on the basis of the value of the flag skip the execution of first four request…

First thought that comes to our mind is to have this flag comparision in the Pre-request script of the very first request, e.g.
Request1 → Pre-request Script :- Do something like this:
if (iterationCount > 1){
postman.setNextRequest(“NameOfYourFifthRequest”);
}

But, unfortunately, setNextRequest is not supported in the Pre-request Script of a request. You can do that only in the Test tab. However, doing this in the Test tab of your first request wont solve our purpose…Right?

So, the solution here is to use a dummy request as your first request. Add this comparision logic in the test tab of this dummy request instead of the First Request.

Where to find this dummy request ??
Postman has several echo endpoints which are dummy requests. You can use one of those GET echo endpoint as your first request.
I remember of having Postman Echo collection by default when I launched postman for the first time. If you dont find it in your case then you can easily find it online in postman docs… https://learning.postman.com/docs/developer/echo-api/
So, the sequence of request in your collection will look like :-

  • Postman GET Echo
  • Req1
  • Req 2
  • Req 3
    …
    …

In the Test tab of Postman GET Echo :-
if (iterationCount > 1){
postman.setNextRequest(“NameOfYourFifthRequest”);
}

Also, now you dont need to create a Collection Variable to maintain the count of iteration. Postman has an inbuilt counter for iterations :slight_smile:
It can be accessed by :- pm.info.iteration

I hope the solution is clear now.

2 Likes

Thanks, @nishant.shah2

I understand the concept now. It is working in my case. However, with this hack, the dummy postman echo request will run in each iteration.

I have put the condition like this,

if(iteration>0){

postman.setNextRequest(“Name of my request”);

}

Thanks for finding the time and explaining the concept in detail… Really appreciate it :slight_smile:

Best,
Vaibhav Sharma

1 Like