Reporter options (CLI, HTML, JUnit) in Postman - Build an Automation Script
import { exec } from 'child_process'; import { promises as fs } from 'fs'; import assert from 'assert'; const collectionFile = 'sample_collection.json'; const htmlReport = 'report.html'; const junitReport = 'junit-report.xml'; async function runNewmanCLI() { return new Promise((resolve, reject) => { exec(`newman run ${collectionFile}`, (error, stdout, stderr) => { if (error) return reject(error); resolve(stdout); }); }); } async function runNewmanHTML() { return new Promise((resolve, reject) => { exec(`newman run ${collectionFile} -r html --reporter-html-export ${htmlReport}`, (error) => { if (error) return reject(error); resolve(); }); }); } async function runNewmanJUnit() { return new Promise((resolve, reject) => { exec(`newman run ${collectionFile} -r junit --reporter-junit-export ${junitReport}`, (error) => { if (error) return reject(error); resolve(); }); }); } async function testReporters() { // Run CLI reporter and check output const cliOutput = await runNewmanCLI(); assert(cliOutput.includes('iterations'), 'CLI output missing iterations count'); assert(cliOutput.includes('requests'), 'CLI output missing requests count'); assert(cliOutput.includes('tests'), 'CLI output missing tests count'); assert(cliOutput.includes('assertions'), 'CLI output missing assertions count'); // Run HTML reporter and verify file await runNewmanHTML(); const htmlContent = await fs.readFile(htmlReport, 'utf-8'); assert(htmlContent.includes('<html'), 'HTML report missing <html> tag'); // Run JUnit reporter and verify file await runNewmanJUnit(); const junitContent = await fs.readFile(junitReport, 'utf-8'); assert(junitContent.includes('<testsuite'), 'JUnit report missing <testsuite> tag'); // Cleanup await fs.unlink(htmlReport); await fs.unlink(junitReport); console.log('All reporter tests passed'); } // Execute test testReporters().catch(err => { console.error('Test failed:', err); process.exit(1); });
This script uses Node.js to automate running Newman with different reporters.
First, it runs Newman with the default CLI reporter and checks the output text for key words like 'iterations' and 'tests' to confirm the summary is shown.
Next, it runs Newman with the HTML reporter and exports the report to 'report.html'. It reads the file and asserts it contains the <html> tag, confirming a valid HTML report.
Then, it runs Newman with the JUnit reporter and exports to 'junit-report.xml'. It reads this file and asserts it contains the <testsuite> tag, confirming a valid XML report.
Finally, it cleans up the generated report files to keep the environment clean.
Assertions ensure the test fails if any expected output is missing, giving clear feedback.
Now add data-driven testing to run the Newman reporters on 3 different Postman collections