Using multiple data files in Postman CLI tests

Hi,

We are trying to increase our usage of Postman for API testing, and as part of this we are coming up against some difficulties, which I was hoping you would be able to help us with, and give us some advice on the best way to achieve what we are looking to do.

We want to create a set of tests, which will build the data required for other tests which will be run, as part of this we want to be able to use multiple JSON data files with the commands.

The workflow would be something along the lines of:

  1. Run a test to create a new network - this uses a JSON data file for the data to be included in the request, from the response we want to get an auth code
  2. Using the auth code from test 1, we want to call our authenticate method on our API, which returns an auth token
  3. Using the auth token from test 2, we want to create a number of users on our network, this would use another JSON file, with an array of objects, and we would want to iterate over the test multiple times

This is a very small section of the tests, but you can see we need to be able to share data taken from the responses of each test, and also use different JSON files for data, which will have different numbers of objects in them.

The postman support team pointed us towards Flows for this, but Flows have their own issues, where you can’t re use blocks across flows, or run multiple flows, you also can’t use external files as inputs for flow variables.

:no_entry: Do NOT include sensitive information like auth tokens or passwords.

Semantics, but it sounds like you have three requests that may or may not have some tests attached.

  1. Create Network (returns an auth code).
  2. Authenticate using the auth code from the first request. Returns an auth token.
  3. Create Users (which requires the auth token from step 2).

You can’t have two data files, so you either need to combine this information into a single data file or use another method for one of the data sets. For example, use an environment to store the auth data and then send the relevant environment variables as part of the command line.

If you combine the data, you could have a CSV data file with only one line of data.

You would have column headers for your auth data, and a single column for your array, which you should store as a stringified version of your array. You could also do this as a JSON data file. Either would work.

In the pre-request script for step 1, you would retrieve the auth columns data by its header name, and then split it out into the appropriate variables for the request. In the tests tab, you would parse the response and save the auth code to an environment or collection variable.

You should also save the array column to an environment or collection variable. Which will be used for step 3. You should be done with the data file at this point.

Request 2 would just use the environment variables for the auth code. No need for a pre-request script. You should have tests to ensure you get an auth token and use setNextRequest = null if you don’t get one.

To loop over request 3, you will use a JavaScript function called shift() which is available on arrays. You will retrieve the array you saved in the pre-request script for step 1. The shift() function will retrieve the first element in that array and then delete it from the array. You would save that to a variable to used in the current request, and then re-save the array over the top of the environment variable. This should now have one less item in the array.

In the tests tab for the request. You will retrieve the array again, and use an IF statement with setNextRequest to keep looping on the same request while the array is larger than 0.

Please search the forum for examples on looping with setNextRequest and array.shift().

This approach does require you to have a reasonable core understanding of JavaScript.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.