noitib
noitib

Reputation: 159

In a Postman collection, one of the services only needs to be called once

I'm designing a fairly simple collection, with two services in the collection:

I've managed to get the token from service #1 and set it into a variable, and use said token in Request #2. I then use a csv file in Runner to call service #2 using different arguments.

What I'm trying to achieve is to run service #1 only once, instead of running as many times as the number of lines on the CSV. Is it possible?

Upvotes: 0

Views: 452

Answers (2)

Tim Hallbeck
Tim Hallbeck

Reputation: 651

setNextRequest() aka 'goto' should never be used in any structured language. That is putting control level functionality into individual tests and will lead on day 1 to spaghetti code. Some basics on collections and variables in Postman and Newman that I find make things easier:

  • Use pm.environment.get() & set() instead of pm.collection so your variables are accessible by everything (essentially global scope)
  • Do your Auth as the first call, and save your auth Token as an env var for use by everything else (sound like you have that already).
  • Put your shared / common functions at the bottom of your Auth request pre-script as utils so they get run once, then are reusable as well.
  • For data driven tests, use 2 collections: the first only has your Auth, the second has everything else. So you'll run the 1st collection with no data file, then the 2nd collection with your .csv file as the data source. Your Token is a pm.environment var so will be visible by ALL collections.

Yes, this means running multiple collections, but that's ok because you'll eventually have multiple .csv files for multiple kinds of tests anyway.

I put all my shared functions (today(). todayPlus1Week(), etc) with my Auth so all the global vars that get used later eg. {{today}}, {{tomorrow}}, {{todayPlus1Week}} get set exactly once, but also can be refreshed will a simple call by any request if for example you have a huuuge data set and it will take a long time to run.

For examples of this, google "postman utils examples" or start here: How to Write Global Functions in Postman

Upvotes: -1

Christian Baumann
Christian Baumann

Reputation: 3434

You can use postman.setNextRequest("request #2"); in the Tests script of request #2 to skip the execution of request #1 after the first run.

But you also need to add a condition to only do that while pm.info.iteration is smaller than your amount of required executions to avoid an endless loop.

Upvotes: -1

Related Questions