Automating API Testing with Newman in Postman

Introduction

In API realms, precision is vital, and efficiency holds the key, Manual testing falters, automation ensures a strategy that's sleek. Postman's Newman, a potent command-line tool in this parade, Automates collections, running tests with reports finely displayed.

What is Newman?

Newman is a command-line collection runner for Postman. It allows you to run Postman collections directly from the command line, making it ideal for continuous integration and continuous deployment (CI/CD) pipelines and other automated testing scenarios.

Getting Started with Newman

Step 1: Install Newman

If you haven't installed Newman yet, you can do so using npm (Node Package Manager). Open your terminal or command prompt and run the following command:

npm install -g newman

Step 2: Export a Postman Collection

Before using Newman, you need a Postman collection. If you don't have one, create a collection in the Postman app and save it. Then, export it as a JSON file.

Step 3: Run Newman

Navigate to the directory where your Postman collection JSON file is located. Open your terminal or command prompt and run the following command:

newman run your_collection.json

please this blog to learn Data Driven Testing in Postman. I am using same code for testing.

Newman Commands

  1. Run a Collection:

     newman run your_collection.json
    

    Replace your_collection.json with the actual filename of your exported Postman collection.

  2. Specify an Environment:

     newman run your_collection.json --environment your_environment.json
    

    Use this command to run a collection with a specific environment file.

  3. Specify a Data File for Iteration:

     newman run your_collection.json --iteration-data your_data_file.csv
    

    If your collection uses data files for iteration, use this command to specify the data file.

  4. Set Global Variables:

     newman run your_collection.json --global-var "variable_name=value"
    

    Set global variables to be used across the entire collection run.

  5. Generate a Basic HTML Report:

     newman run your_collection.json --reporters html
    

    Create a basic HTML report in the current directory.

  6. Specify Report Output File and Format:

     newman run your_collection.json --reporters cli,html --reporter-html-export custom-report.html
    

    Generate both console (cli) and HTML reports with a custom filename.

  7. Suppress Console Output:

     newman run your_collection.json --silent
    

    Run the collection silently without showing detailed console output.

  8. Specify Iteration Count:

     newman run your_collection.json --iteration-count 3
    

    Run the collection for a specific number of iterations.

  9. Run a Single Request by Name:

     newman run your_collection.json --folder "Folder Name" --request "Request Name"
    

    Execute a specific request within a collection using the folder and request names.

  10. Export Collection Run Data:

    newman run your_collection.json --export-environment exported_environment.json
    

    Export the environment data after the collection run.

  11. Ignore SSL Certificates:

    newman run your_collection.json --insecure
    

    Disable SSL certificate validation during the collection run.

Newman Reports

One of the significant advantages of using Newman is its ability to generate detailed reports. These reports provide insights into the test run, including the number of requests executed, assertions passed or failed, and response times. Let's explore how to generate a Newman report.

1. Basic HTML Report

Running a collection with Newman generates a basic HTML report by default. This report includes essential information such as request details, assertions, and response times. To create a basic HTML report, use the following command:

newman run your_collection.json -r html

output:

2. Console (CLI) Report

In addition to HTML reports, Newman provides a console report that displays test results directly in the terminal or command prompt. This is useful for quick insights during the test run. To include a console report, use the following command:

newman run your_collection.json -r cli

output:

3. Customizing Reports

Customizing reports allows you to tailor the output to your specific needs. You can export reports in different formats and specify filenames. For example:

newman run your_collection.json -r cli,html -r-html-export custom-report.html

This command generates both a console (cli) report and an HTML report named custom-report.html.

4. Newman-Reporter-Htmlextra

Newman-Reporter-Htmlextra is an additional reporting option that enhances HTML reports with more features and visualizations. To use this reporter, you need to install it first:

npm install -g newman-reporter-htmlextra

Once installed, you can generate an enhanced HTML report using the following command:

newman run your_collection.json --reporters htmlextra

This will create an HTML report with additional features like pie charts, request/response visualizations, and an overall improved layout.

output:

Conclusion

In this blog post, we've covered the basics of using Newman in Postman for API testing automation. From installation to running collections and generating reports, Newman provides a seamless solution for incorporating automated API testing into your development workflow. .