Collection Runner and data-driven tests
The Collection Runner runs a collection or folder sequentially, and a data-driven run attaches a CSV or JSON file where one row equals one iteration. File columns become data-scope variables, so the iteration count equals the row count and new boundary or negative values are added as a row without script changes. A value in quotes arrives as a string, so comparing it with a status needs Number(...).
API · Runner, data-driven tests and Newman
One request must run against five data sets. Do you copy it five times?
A transfer needs five amounts checked: a normal one, zero, negative, too large and a currency that does not exist. The easiest way is five copies of the request with different bodies. A month later a header is fixed in two copies and not in three, and a new boundary needs a sixth copy. The Collection Runner works differently: one request and the data apart.
How it works and why it is designed so
The Collection Runner runs a whole collection or folder sequentially: with the chosen environment, the number of iterations and a delay between requests. The outcome looks like a pass/fail table for each pm.test. For a data-driven run you attach a CSV or JSON file: one row is one iteration and the columns become variables of the data scope. The number of iterations then equals the number of rows and the whole set of requests runs for each row. This separates behaviour from data: new boundaries are added as a row in the file without changing scripts.
One request, many data rows
Example: five cases from one request
The data file: the input, the currency and the expected status in one row.
amount,currency,expectedStatus
150,UAH,201
0,UAH,400
-10,UAH,400
1000000000,UAH,422
150,XXX,400
In the request body variables from the file are substituted like any others. A number is written without quotes and a string with quotes:
{ "amount": {{amount}}, "currency": "{{currency}}" }
And the check reads the same row through pm.iterationData. Number(...) is needed because values from a file should not be assumed to be numbers: comparing the status with the string '201' will not pass:
// Post-response of POST {{baseUrl}}/transfers
const amount = pm.iterationData.get('amount');
const expected = Number(pm.iterationData.get('expectedStatus'));
// the iteration value in the name: the report shows which row failed
pm.test('amount=' + amount + ' -> ' + expected, () => {
pm.response.to.have.status(expected);
});
The test name contains the iteration value, so the report shows exactly which row failed. Without it five identical "Status is 201" explain nothing. How to work with JSON files, types and clean data between iterations is covered in the lesson on data-driven in depth.
Limits and alternatives
The Runner is handy for manual and semi-manual runs, but it runs from Postman. For CI the same run is done by Newman or the Postman CLI (see the lesson on Newman). A file row sets only values: complex logic ("for role A expect 403, for B 200") is better encoded in an expected-result column than in script branches. When there are hundreds of cases and generators are needed, code with parameterised tests is easier to maintain (the lesson on Postman's limits).
Common mistakes
- Identical test names. The report has five "Status is 201" and it is unclear which row failed;
- Comparing with a type that is not there. A quoted value (
"201") arrives as a string, andto.have.statuscompares a string with the status text (Created), not the code: convert throughNumber; - A file column matches an environment variable name. The data scope is narrower and silently overrides the environment;
- Data that depends on row order. Row 2 relies on what row 1 created. Iterations must be independent;
- A file with empty rows, an uneven column count or Windows line endings. Column names or the iteration count may differ from what you expect: compare them with the report of the first run.
Check yourself
A CSV has six data rows. How many iterations does the Runner perform and how many times does each request of the folder run?
Answer
Six iterations, and each request of the folder runs once per iteration, that is six times. The file columns are available as data-scope variables only within their own iteration.
The CSV holds the status in quotes ("201") and the test pm.response.to.have.status(pm.iterationData.get('expectedStatus')) fails although the status is right. Why?
Answer
A quoted value arrives as a string, and status() compares a string with the status text, not the code (Newman converts only unquoted numbers itself). Use Number(...) (in a JSON file the number would stay a number). The test name is worth composing from the row values so the cause is visible.