0
0
Postmantesting~15 mins

Data file with Newman in Postman - Build an Automation Script

Choose your learning style9 modes available
Run Postman collection with data file using Newman
Preconditions (3)
Step 1: Open terminal or command prompt
Step 2: Run Newman command with the collection file and data file using --iteration-data option
Step 3: Wait for Newman to execute all iterations
Step 4: Observe the test run results for each data set
✅ Expected Result: Newman runs the collection once for each data set in the data file and shows pass/fail results for each iteration
Automation Requirements - Newman CLI
Assertions Needed:
Verify Newman runs all iterations equal to the number of data rows
Verify each iteration passes all tests in the collection
Best Practices:
Use --iteration-data option to specify data file
Use JSON or CSV format for data file
Check exit code of Newman command for success
Use descriptive names for data sets in the file
Automated Solution
Postman
const { exec } = require('child_process');

const collectionFile = 'path/to/collection.json';
const dataFile = 'path/to/data.json';

exec(`newman run ${collectionFile} --iteration-data ${dataFile} --reporters cli,json --reporter-json-export newman-report.json`, (error, stdout, stderr) => {
  if (error) {
    console.error(`Error executing Newman: ${error.message}`);
    process.exit(1);
  }
  if (stderr) {
    console.error(`Newman stderr: ${stderr}`);
  }
  console.log(`Newman output:\n${stdout}`);

  // Simple check for iterations run
  const iterationsMatch = stdout.match(/iterations:\s*(\d+)/i);
  if (iterationsMatch) {
    const iterations = parseInt(iterationsMatch[1], 10);
    console.log(`Iterations run: ${iterations}`);
  } else {
    console.warn('Could not find iterations count in output');
  }

  // Check for failures
  const failuresMatch = stdout.match(/failed:\s*(\d+)/i);
  if (failuresMatch) {
    const failures = parseInt(failuresMatch[1], 10);
    if (failures === 0) {
      console.log('All tests passed for all iterations.');
    } else {
      console.error(`There are ${failures} test failures.`);
      process.exit(1);
    }
  } else {
    console.warn('Could not find failures count in output');
  }
});

This Node.js script runs Newman CLI using child_process.exec to execute the Postman collection with the data file.

The --iteration-data option tells Newman to run the collection once per data row.

The script captures the output and parses it to find how many iterations ran and how many tests failed.

If there are any failures, the script exits with error code 1 to indicate test failure.

This approach automates running the collection with multiple data inputs and verifies the results programmatically.

Common Mistakes - 4 Pitfalls
Not using --iteration-data option and running collection only once
Using incorrect path or filename for collection or data file
Ignoring Newman exit code and output
Using data file with wrong format or invalid JSON/CSV
Bonus Challenge

Now add data-driven testing with 3 different input data sets in the data file and verify all iterations pass

Show Hint