Write scripts that know where they are: pm.info and iteration data
A script on one request is simple. A script on a folder of twenty requests, run once per row of a 500-row CSV, needs to know where it is: which request, which iteration, which row. Two objects tell it.
pm.info
console.log(pm.info.eventName); // "prerequest" or "test"
console.log(pm.info.requestName); // "Create order"
console.log(pm.info.iteration); // 0, 1, 2 … (counts from 0)
console.log(pm.info.iterationCount); // 3 when you run 3 iterations or 3 data rowsA single Send is iteration 0 of 1.
Make each iteration unique
When you run a collection three times, the second run of "Create user" shouldn't collide with the first. Use the iteration number:
// Create user — pre-request
pm.environment.set('email', `load-${Date.now()}-${pm.info.iteration}@example.test`);And tag each request, so you can find it in the server logs:
pm.request.headers.set('X-Test-Run', `${pm.info.requestName} #${pm.info.iteration + 1}/${pm.info.iterationCount}`);Values that scripts set don't carry over from one iteration to the next: every iteration starts fresh. So anything an iteration needs, such as a token, it gets itself, and the iterations stay independent.
One folder script, many requests
A folder's post-response script runs after every request inside it. pm.info.requestName lets it treat some requests differently:
// Folder "Orders" — post-response
const name = pm.info.requestName;
pm.test(`${name}: no server error`, () => pm.expect(pm.response.code).to.be.below(500));
if (name.startsWith('List ')) {
pm.test(`${name}: has a data array`, () => pm.expect(pm.response.json().data).to.be.an('array'));
}Putting the request name in the test name also makes JUnit reports and CI annotations easier to read. See name API tests so a failure explains itself.
pm.iterationData
With a data file (Iteration data in the app's runner, --data in the CLI), each row is one iteration:
email,plan,expectedStatus
[email protected],pro,201
[email protected],unknown,400
,pro,400In requests, the columns are variables: {"email": "{{email}}", "plan": "{{plan}}"}. In scripts, read the row:
const row = pm.iterationData.toObject();
const expected = Number(pm.iterationData.get('expectedStatus'));
pm.test(`row ${pm.info.iteration + 1} (${row.email || 'no email'}, ${row.plan}): status ${expected}`, () => {
pm.response.to.have.status(expected);
});Every value in a row is text, even numbers, so convert with Number(...) before comparing with a status code.
Optional columns
has tells "column missing" apart from "column empty":
if (pm.iterationData.has('coupon') && pm.iterationData.get('coupon') !== '') {
pm.test('discount applied', () => pm.expect(pm.response.json().discount).to.be.above(0));
}Outside a data run
On a single Send, pm.iterationData is empty: get returns undefined, toObject() returns {}. Give scripts a default so the request still works when you send it by hand:
const expected = Number(pm.iterationData.get('expectedStatus') ?? 201);Row values vs environment values
A row value wins over the environment, and --var on the command line wins over the row. So a column named baseUrl would override the environment's baseUrl for that row. Name columns so they don't clash, unless overriding is what you want.