You already know data-driven testing from Postman's Collection Runner. Newman supports the exact same thing. Same CSV and JSON files. Same {{variables}}. But from the command line.
userId,expectedName,expectedStatus
1,Leanne Graham,200
2,Ervin Howell,200
3,Clementine Bauch,200
999,,404# Run with CSV data file — one iteration per row
newman run collection.json \
-e environments/dev.json \
-d data/test-data.csv
# Newman automatically sets iterations = number of data rows
# 4 rows in CSV = 4 iterations[
{
"username": "testuser1",
"email": "test1@example.com",
"expectedStatus": 201
},
{
"username": "",
"email": "invalid",
"expectedStatus": 400
},
{
"username": "admin",
"email": "admin@example.com",
"expectedStatus": 409
}
]# Run with JSON data file
newman run collection.json \
-e environments/staging.json \
-d data/test-data.json# Run 10 iterations (without a data file)
# Useful for stress testing or testing with random data from pre-request scripts
newman run collection.json -n 10
# Run only the first 3 rows of a data file
newman run collection.json -d test-data.csv -n 3
# Run iterations with a delay between each (in ms)
newman run collection.json -d test-data.csv --delay-request 500Combine -d and -n together: if your CSV has 100 rows and you pass -n 10, Newman runs only the first 10 rows. Handy for quick smoke tests using a subset of your full test data.
# Full data-driven run with HTML report
newman run collection.json \
-e environments/staging.json \
-d data/users-test-data.csv \
-r cli,htmlextra \
--reporter-htmlextra-export reports/data-driven-report.html
# The HTML report shows each iteration separately
# You can see which data rows passed and which failedLarge data files (1000+ rows) can make Newman slow and the HTML report huge. For massive data sets, consider splitting into smaller files and running them in parallel, or use the JSON reporter instead of HTML.
Q: How do you run data-driven tests with Newman? What flags do you use?
A: Use the -d flag followed by the path to your CSV or JSON data file: newman run collection.json -d test-data.csv. Each row in the CSV (or each object in the JSON array) becomes one iteration. Newman automatically sets the iteration count to match the data file. Column headers become data variables accessible via {{columnName}} in requests and pm.iterationData.get("columnName") in scripts. You can limit iterations with -n flag (e.g., -d data.csv -n 5 runs only the first 5 rows). Combine with -r htmlextra to get a report showing results per iteration.
Key Point: Use -d for data files (CSV or JSON). Use -n for iteration count. Each data row = one iteration. Combine with reporters for per-iteration results.