QA Interview Hub · Knowledge base · Quizzes · Українська

API › Runner, data-driven tests and Newman

Data-driven in depth: files, iterations, data isolation

In a data-driven run, iterations share environment and collection variables, so each creates its own data, cleans it up and resets temporary variables. Otherwise a test passes on leftovers from the previous row. CSV suits simple flat tables without types, while JSON keeps numbers, booleans and nested objects. Unique fields are generated in the pre-request script.

API · Runner, data-driven tests and Newman

The second CSV row fails because the first left data in variables. How do you isolate iterations?

The first row of the file created a user and wrote its id into a variable. The second row gets a 409 on a duplicate email and the third "finds" a foreign user and passes by accident. The report is green or red depending on the order. The Runner is not to blame: iterations share variables and isolation is your job. This lesson covers the file format choice, the content of the table and data cleanliness.

How it works and why it is designed so

CSV or JSON. CSV is simple and can be kept in a spreadsheet, but it has no types: do not rely on automatic conversion to a number or a boolean and convert explicitly (Number(...)). JSON preserves types (number, boolean, null) and nested objects, so it suits complex request bodies. A typical mistake: in JSON the field "ok": true is a boolean while the test compares it with the string 'true'.

What the table should contain. Input values, the expected status and, if needed, expected response fields. Rows are boundary values, equivalence classes, negative cases and a permission matrix ("role, action, expected code"). Compose the test name from the row values so the report shows what exactly failed.

Data isolation between iterations. Environment and collection variables live between iterations: a value from iteration 1 is visible in iteration 2, and a test that relies on it may pass by accident. Local variables (pm.variables) live within a run, not one request. So each iteration must be responsible for its own data: create, use, clean up.

Iterations without isolation and with it

Bad

Data from iteration 1 stayed

An environment variable lives between iterations: iteration 2 sees a foreign id and may pass by accident.

Good

Each iteration creates and cleans its own

Unique fields are generated, temporary variables are reset, what was created is deleted at the end.

Figure 1. The same rows give different results if iterations share leftovers.

Example: a JSON file and isolation scripts

In JSON numbers stay numbers, and the request body reads them as { "amount": {{amount}}, "currency": "{{currency}}" }: a number without quotes, a string with quotes.

[
  { "amount": 150, "currency": "UAH", "expectedStatus": 201 },
  { "amount": 0, "currency": "UAH", "expectedStatus": 400 },
  { "amount": 150, "currency": "XXX", "expectedStatus": 400 }
]

Isolation scripts: unique fields are generated in a pre-request (otherwise the second row gets a 409 on a duplicate), the id for cleanup is stored and reset at the end of the iteration:

// Pre-request: a unique value for each iteration
const n = pm.info.iteration;            // iteration number from 0
pm.variables.set('email', 'user' + n + '+' + Date.now() + '@example.test');

// Post-response: keep the id for cleanup
pm.environment.set('createdId', pm.response.json().id);

// Post-response of the last request of the iteration (after DELETE in Cleanup)
pm.environment.unset('createdId');      // do not leave it for the next iteration

Data created by a test is cleaned up at the end of the iteration: a Cleanup folder with DELETE by the stored id. A failed test does not stop the run, so cleanup will execute. An exception: in Newman with the --bail flag the run stops after the first failure and Cleanup may not run. Do not rely on data that is already in the database: a test creates its own. For cleanup and priorities see the lesson on test data.

Limits and alternatives

The iteration flow can be controlled: pm.info.iteration and pm.info.iterationCount give the number and the count, and pm.execution.setNextRequest changes the request order. Complex branching is better not to build in Postman: it reads poorly and is harder to test, and then code gives a cleaner result. The Runner's run settings have a variable-persistence option ("Keep variable values"): it decides whether the changes remain after the run. Check its state if the next run picks up leftovers of the previous one.

Common mistakes

  • Leftovers in variables between iterations. A test passes thanks to foreign data. Reset temporary variables with unset;
  • A fixed email or order number in all rows. The second row gets a 409. Generate unique values;
  • Comparing a boolean with a string. In JSON it is true while the test expects 'true';
  • Created data without cleanup. The database grows and later runs break on duplicates;
  • Dependence on another iteration. A test that waits for the previous row's data cannot be run alone.

Check yourself

Iteration 1 stored createdId in an environment variable, iteration 2 did not create one, yet the test on createdId passed. Why is that dangerous?

Answer

Environment variables live between iterations, so iteration 2 used a foreign value and the green test proved nothing. Creation and cleanup must be in every iteration and temporary variables are reset with unset.

When is a JSON file better and when is CSV?

Answer

CSV for simple flat tables kept in a spreadsheet. JSON when types (number, boolean, null) or nested objects in the request body are needed.

In an interview. Junior: For data-driven runs I use CSV for simple tables and JSON when types or nested bodies are needed. I compose the test name from the row values and make sure iterations do not share data. Middle/Senior: Each iteration generates its own unique fields, cleans up what it created and resets temporary variables, because environment variables live between iterations. The table rows are boundaries, equivalence classes and a permission matrix, and complex branching goes into code.

More articles in this chapter