Bird
Raised Fist0
Postmantesting~15 mins

Reporter options (CLI, HTML, JUnit) in Postman - Build an Automation Script

Choose your learning style10 modes available
Verify Postman Newman CLI, HTML, and JUnit reporters generate correct output
Preconditions (3)
Step 1: Run Newman with CLI reporter on 'sample_collection.json'
Step 2: Verify the CLI output shows test run summary with pass/fail counts
Step 3: Run Newman with HTML reporter and specify output file 'report.html'
Step 4: Verify 'report.html' file is created and contains HTML report content
Step 5: Run Newman with JUnit reporter and specify output file 'junit-report.xml'
Step 6: Verify 'junit-report.xml' file is created and contains valid XML with test results
✅ Expected Result: Newman runs the collection and produces correct CLI output, HTML report file, and JUnit XML report file with accurate test results
Automation Requirements - Node.js with child_process to run Newman CLI
Assertions Needed:
CLI output contains 'iterations', 'requests', 'tests', 'assertions', 'failed' counts
HTML report file exists and contains '<html>' tag
JUnit XML report file exists and contains '<testsuite>' tag
Best Practices:
Use child_process.exec or spawn to run Newman commands
Check file existence and read content for validation
Use assertions to verify output correctness
Clean up generated report files after test
Automated Solution
Postman
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.

Common Mistakes - 3 Pitfalls
Not waiting for Newman process to finish before checking output files
Hardcoding absolute file paths without considering environment
Checking only file existence without validating content
Bonus Challenge

Now add data-driven testing to run the Newman reporters on 3 different Postman collections

Show Hint

Practice

(1/5)
1. Which reporter option in Postman is best for quickly viewing test results directly in the terminal?
easy
A. JSON reporter
B. HTML reporter
C. CLI reporter
D. JUnit reporter

Solution

  1. Step 1: Understand reporter types

    CLI reporter shows results in the terminal quickly, HTML creates detailed reports, and JUnit is for integration.
  2. Step 2: Match reporter to quick terminal output

    CLI reporter is designed for quick terminal output, so it fits the requirement.
  3. Final Answer:

    CLI reporter -> Option C
  4. Quick Check:

    Quick terminal output = CLI reporter [OK]
Hint: CLI means command line, perfect for terminal output [OK]
Common Mistakes:
  • Confusing HTML reporter as quick terminal output
  • Choosing JUnit reporter for terminal display
  • Thinking JSON reporter is default quick output
2. Which of the following is the correct syntax to run a Postman collection with an HTML reporter using the CLI?
easy
A. newman run collection.json --reporter html
B. newman run collection.json --reporter-html
C. newman run collection.json -r html
D. newman run collection.json --reporters=html

Solution

  1. Step 1: Recall Postman CLI reporter syntax

    The correct syntax uses '--reporter' followed by the reporter name without extra symbols.
  2. Step 2: Check each option

    newman run collection.json --reporter html uses '--reporter html' which is correct. newman run collection.json --reporter-html and D have incorrect flags, and C uses shorthand '-r' which is invalid.
  3. Final Answer:

    newman run collection.json --reporter html -> Option A
  4. Quick Check:

    Correct flag is '--reporter html' [OK]
Hint: Use '--reporter html' exactly for HTML reports in CLI [OK]
Common Mistakes:
  • Using '--reporter-html' instead of '--reporter html'
  • Using shorthand '-r' which is not supported
  • Adding '=' sign in '--reporters=html'
3. What will be the output when running this command?
newman run test_collection.json --reporter junit
Choose the best description.
medium
A. Displays test results in the terminal in JUnit style
B. Generates a JUnit XML report file for integration with CI tools
C. Creates an HTML report and opens it in the browser
D. Runs tests silently without any output

Solution

  1. Step 1: Understand JUnit reporter purpose

    JUnit reporter generates XML files compatible with CI/CD tools for test result integration.
  2. Step 2: Match command output

    The command uses '--reporter junit' which creates the XML report file, not terminal display or HTML report.
  3. Final Answer:

    Generates a JUnit XML report file for integration with CI tools -> Option B
  4. Quick Check:

    JUnit reporter = XML report file [OK]
Hint: JUnit reporter creates XML files for CI tools, not terminal output [OK]
Common Mistakes:
  • Thinking JUnit reporter shows output in terminal
  • Confusing JUnit with HTML reporter
  • Assuming no output is generated
4. You run this command but get an error: newman run collection.json --reporter html junit. What is the likely cause?
medium
A. Missing comma between reporter names
B. Collection file name is invalid
C. HTML reporter is not supported in Newman
D. Incorrect flag usage for multiple reporters

Solution

  1. Step 1: Check syntax for multiple reporters

    Newman requires multiple reporters to be specified with the '--reporters' flag and comma-separated list.
  2. Step 2: Analyze given command

    The command uses '--reporter' once and lists reporters without commas, which is invalid syntax.
  3. Final Answer:

    Incorrect flag usage for multiple reporters -> Option D
  4. Quick Check:

    Use '--reporters html,junit' for multiple reporters [OK]
Hint: Use '--reporters html,junit' for multiple reporters [OK]
Common Mistakes:
  • Using '--reporter' instead of '--reporters' for multiple
  • Not separating reporter names with commas
  • Assuming HTML reporter is unsupported
5. You want to run a Postman collection and generate both a detailed HTML report and a JUnit XML report for CI integration. Which command correctly achieves this?
hard
A. newman run collection.json --reporters html,junit
B. newman run collection.json --reporter html --reporter junit
C. newman run collection.json --reporters html;junit
D. newman run collection.json --reporters html junit

Solution

  1. Step 1: Understand multiple reporter syntax

    Newman uses '--reporters' flag with comma-separated reporter names to run multiple reporters.
  2. Step 2: Evaluate each option

    newman run collection.json --reporters html,junit uses correct syntax with '--reporters html,junit'. newman run collection.json --reporter html --reporter junit uses '--reporter' twice which is invalid. Options A and D use incorrect separators.
  3. Final Answer:

    newman run collection.json --reporters html,junit -> Option A
  4. Quick Check:

    Multiple reporters = '--reporters html,junit' [OK]
Hint: Use '--reporters' with comma-separated names for multiple reports [OK]
Common Mistakes:
  • Using multiple '--reporter' flags instead of one '--reporters'
  • Separating reporters with semicolon or space instead of comma
  • Assuming only one reporter can be used
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard