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!');
    });
})

Hope it helps :smiley: https://praveendavidmathew.medium.com/postman-trick-and-tips-how-to-run-a-specific-iteration-data-from-newman-ac0cbaf9effb

5 Likes