As an API developer, you are likely familiar with Postman, a popular tool for testing and automating API requests. Postman Newman has been a go-to tool for many developers for years, but it's not without its limitations.
In this guide, we will cover the installation and usage of Newman, a command-line tool designed to run Postman collections. By following this guide, you will learn how to install Newman and execute Postman collections, enabling you to automate your API testing process. A recommends Apidog as a powerful alternative.
What is Newman in Postman?
Newman Postman is a command-line tool that allows developers to run Postman collections and environments in a continuous integration (CI) environment. Postman is a popular API development tool that allows developers to design, test, and document APIs. Newman, on the other hand, is a tool that allows developers to automate API tests using Postman collections and environments.
With Newman Postman, developers can easily automate API tests and ensure that their APIs are working as expected. Newman supports a range of integrations and plugins, making it a versatile tool for API development. However, there are some limitations to Newman Postman that developers should be aware of, such as its complexity to set up and configure slow test execution and cost for large-scale use.
What is the Difference Between Newman and Postman?
Postman and Newman are both API development tools, but they serve different purposes. Postman is a desktop application that allows developers to test APIs with a graphical user interface (GUI). Newman, on the other hand, is a command-line tool that allows developers to run Postman collections and environments in a continuous integration (CI) environment, making it easier to automate API tests.
Another difference between Postman and Newman is their cost. Postman has a free version but also offers paid plans with additional features. Newman, on the other hand, is an open-source tool that is free to use.
How to Run Postman Collection in Newman?
Installing Newman
Newman is built on Node.js. To run Newman, make sure you have Node.js installed. And Node.js >= v16. You can install Node.js with the package manager:
Using NPM is a common and easy way to install Newman. Input the following command in the terminal.
npm install -g newman
By using the "-g" flag, Newman will be installed globally on your system, enabling you to run it from any location.
Running Postman Collections with the Newman
You can use the "newman run" command to indicate which collection to run. It's simple to export your Postman Collection as a JSON file from the Postman, and then execute it using Newman.
newman run mycollection.json
Recommended to Read:
Bonus Tips of Postman Newman
Using Newman with CI/CD
Newman exits with a status code of 0 by default if everything runs smoothly without any exceptions. To ensure that your continuous integration (CI) tools respond to Newman's exit codes and pass or fail a build accordingly, you can configure them accordingly.
Additionally, you can use the "--bail" flag to make Newman stop the run if it encounters a test case error with a status code of 1. This error can then be picked up by your CI tool or build system and acted upon accordingly.
Using Newman as a Node.js library
Newman can also be used as a Node.js library, allowing developers to integrate API testing into their Node.js applications. By using Newman as a library, developers can programmatically run collections, access test results, and integrate testing into their build process.
const newman = require('newman'); // Import Newman module
async function runCollection() {
try {
await newman.run({
collection: require('./sample-collection.json'),
reporters: 'cli'
});
console.log('Collection run complete!');
} catch (err) {
console.error(err);
}
}
runCollection();
This example code uses async/await syntax to make running Newman more concise and readable. It imports the Newman module and defines an asynchronous function named runCollection to run the collection. Within the function, it uses the await keyword to wait for Newman to finish running, and then prints a message indicating that the collection has been run. If an error occurs, it catches and prints the error. Finally, it calls the runCollection() function to run the entire program.
Postman Newman Alternative: Apidog
Apidog provides support for exporting test scenarios to Apidog CLI, Postman, and JMeter formats, making it easy to integrate API testing into your continuous integration process.
The Apidog CLI is designed specifically for running Apidog test scenarios in the command line, providing a streamlined way to execute tests and automate the testing process. By exporting test scenarios to Apidog CLI, developers can easily integrate API testing into their build processes and ensure that APIs are tested thoroughly and consistently.
In addition to Apidog CLI, Apidog also supports exporting test scenarios to Postman and JMeter formats, providing developers with greater flexibility in their testing approach. With these export options, developers can choose the testing tool that best suits their needs and integrate API testing seamlessly into their development workflow.
How to Install Apidog CLI
Prerequisite: At first, you need to install Node.js. Preferably, version >=v16.
Use the command below to install:
npm install -g apidog-cli
You can install Apidog CLI globally via npm on your system, allowing you to execute it from any location.
npm install -g apidog-cli
Here are several commands you can use to confirm whether Apidog CLI has been installed successfully.
node -v && apidog -v && which node && which npm && which apidog
If the installation was successful, executing the commands above will display the version number and installation path immediately.
Running with Real-time Data
Go to the "tests," open your desired API, and switch to the CI/CD page. If you don't have a test scenario yet, just create one.
Set up the environment, test data, loops, intervals, and more. In your CI/CD provider, choose "Command line" and then switch to "Run exported data." Here, you can choose to use the data exported by Newman.
Running the Exported Test Scenarios
apidog run examples/sample.apidog-cli.json -r cli,html,json
The test report will be saved to the apidog-reports directory.
If you want to customize the report, you can use the result in a JSON file to customize the report as needed.
You can also locate the /apidog-reports/ directory in the folder where you executed the CLI. This directory holds the CLI test report in HTML format
Conclusion
By following the steps outlined in this guide, you should now have a good understanding of how to install and run Newman to execute Postman collections. Newman provides a command-line interface that allows for automated API testing, making it a valuable tool for developers working in a continuous integration environment.
However, if you are looking for an alternative to Newman that offers a more robust CI/CD solution, I would highly recommend considering Apidog. In addition to its user-friendly interface for API testing and documentation, Apidog also provides powerful CI/CD functionality. With features like test case exporting, collaboration tools, automatic API documentation generation, and seamless integration with CI/CD pipelines, Apidog provides a comprehensive solution for API development and testing needs.
Whether you choose to continue using Newman or explore Apidog as an alternative, both tools can greatly enhance your API testing process. However, if you are looking for a more comprehensive solution with strong CI/CD functionality, Apidog is definitely worth considering.