Postman trick and tips: How to run a specific iteration data from Newman

There is no inbuilt way to specify a specific iteration row to be executed using Newman, but can be done using Powershell or by using Newman as a library.

The approaches are as below

Powershell:

here we read the actual csv file in the current directory using import csv

Then considers only row 1..2 , if you just want 1 row you can change $a[1..2] to $[1]

$a= Import-Csv .\a.csv
$a[1..2] | Select-Object * | export-csv -Path .\temp.csv -NoTypeInformation
newman run .\test.postman_collection.json -d .\temp.csv

As library:

here we read and split the csv as we want using csv parser and then write it back to a temp file as csv using csv-stringify library.

First, install:

npm install csv
npm install fs
npm i fs-extra
npm install newman

Then use the below code

const newman = require('newman'); // require newman in your project
const stringify = require('csv-stringify')
const parse = require('csv-parse/lib/sync')
const fs = require("fs")
// call newman.run to pass `options` object and wait for callback
let data = fs.readFileSync('./a.csv',
{ encoding: 'utf8', flag: 'r' });
data = parse(data, {
columns: true,
    skip_empty_lines: true
})
console.log(data)
//index doesn't consider header so 0 is first data row
stringify(data.slice(0, 2), {
header: true
}, function (err, output) {

    fs.writeFileSync("temp.csv", output);

    newman.run({
collection: require('./test.postman_collection.json'),
reporters: 'cli',
iterationData: "./temp.csv"
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
})

Data driven testing per request without using data file

Create a environment variable called “csv” and copy the below content and paste it as value:

username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1

Now in pre-request add :

//we use variables as it will get destroyed after the run . So if index returns null then it means it’s the first iteration
if (!pm.variables.get("index")) {
    const parse = require('csv-parse/lib/sync')
//Environmental variable where we copy-pasted the csv content
const input = pm.environment.get("csv");
const records = parse(input, {
columns: true,
skip_empty_lines: true
})
    pm.variables.set("index", 0)
pm.variables.set("records", records)
}
records = pm.variables.get("records")
index = pm.variables.get("index")
//we are setting variable 
if (index !== records.length) {
for (let i of Object.entries(records[index])) {
pm.variables.set(i[0], i[1])
}
pm.variables.set("index", ++index)
pm.variables.get("index")===records.length?null:postman.setNextRequest(pm.info.requestName)
}

Now you can run data driven for that one particular request:

Note that here we used pm.variables. this is because it creates local variable that get destroyed after the collection run completes .

So for each run , first value will be null and then will be set to 0

Eg collection:

https://www.postman.com/collections/eb144d613b7becb22482

use the same data as environment variable content , now run the collection using collection runner or newman.

You can import this collection by clicking file >import >and then choosing links

Output: