Skip to content

How To Use Newman With Postman

API Testing Blog

Leveraging Newman with Postman for Robust API Testing

Newman, a command-line interface runner for Postman collections, is a powerful tool for automating API testing. It allows you to execute your Postman collections directly from the command line, integrate them into CI/CD pipelines, and perform repetitive tests efficiently. This guide explores how to seamlessly integrate Newman with Postman to enhance your API testing workflow.

1. Setting up Newman

Before you start, ensure that you have Node.js and npm (Node Package Manager) installed on your system.

  1. Install Newman: Open your terminal or command prompt and run the following command:

    Terminal window
    npm install -g newman
  2. Verify installation: Execute the following command to confirm Newman is installed:

    Terminal window
    newman -v

    This command should print the Newman version.

2. Generating a Collection in Postman

  1. Open Postman: Launch the Postman application.

  2. Create a new collection: Click on the “Create Collection” button or navigate to the “Collections” tab and click on the “Create Collection” button.

  3. Add requests: Within the collection, define each of your API requests. Include the following details for each request:

    • Request Method: GET, POST, PUT, DELETE, etc.
    • Request URL: The endpoint you want to test.
    • Request Headers: Include any required headers for authorization, content type, etc.
    • Request Body: Provide the necessary data for the request, if applicable.
    • Request Tests: Define assertions to validate responses, such as expecting a specific status code or response content.
  4. Save the collection: Ensure you save your collection for later use.

3. Running Your Collection with Newman

  1. Export the collection: In Postman, go to your collection and click the “Export” button. Select “Collection v2” as the export format.

  2. Open your terminal: Navigate to the directory where you saved the exported collection file (collection.json).

  3. Run Newman: Execute the following command to run your collection:

    Terminal window
    newman run collection.json
  4. Optional: Environment Variables: You can pass environment variables to Newman to dynamically customize your tests. Create an environment file (environment.json) with variables and their values:

    {
    "baseUrl": "https://api.example.com",
    "apiKey": "YourApiKey"
    }

    Then, use the -e flag to specify the environment file during execution:

    Terminal window
    newman run collection.json -e environment.json

Example: Testing a Simple API Endpoint

Postman Collection:

  1. Create a collection: “Simple API Tests”
  2. Add a request:
    • Method: GET
    • URL: {{baseUrl}}/users
    • Headers: None.
    • Tests:
      pm.test("Status code is 200", function () {
      pm.response.to.have.status(200);
      });

Newman Command:

Terminal window
newman run collection.json -e environment.json

Environment File (environment.json):

{
"baseUrl": "https://api.example.com"
}

This example demonstrates how to test a GET request to /users endpoint, ensuring the response code is 200 using Newman with an environment variable for the base URL.

4. Integrating Newman with CI/CD

Newman seamlessly integrates with CI/CD pipelines, enabling automated testing as part of your development workflow.

  1. Choose your CI/CD platform: Jenkins, CircleCI, Travis CI, GitHub Actions, etc.
  2. Setup the environment: Ensure Node.js and Newman are installed on the CI/CD platform.
  3. Add a test stage: Include a stage in your pipeline that executes the Newman command to run your Postman collection.

Here’s an example using a Jenkinsfile for Jenkins:

pipeline {
agent any
stages {
stage('Build') {
steps {
...
}
}
stage('API Test') {
steps {
sh 'newman run collection.json -e environment.json'
}
}
}
}

5. Advanced Newman Features

  1. Reporting: Generate detailed reports for analysis using the -r flag with options like html, json, junit formats:

    Terminal window
    newman run collection.json -r html -o report.html
  2. Globbing: Run multiple collections using wildcards:

    Terminal window
    newman run *.json
  3. Repetition: Run your tests multiple times using the -n flag:

    Terminal window
    newman run collection.json -n 5
  4. Data-Driven Testing: Use data-driven testing for scenarios involving different data sets with the --iterationData option.

Conclusion

Newman offers a robust and efficient way to automate API testing using Postman collections. Its seamless integration with CI/CD, advanced features, and command-line control empower developers and testers to build reliable and scalable API testing workflows. By following this comprehensive guide, you can effectively integrate Newman with Postman for robust API testing and continuous integration.

API Testing Blog