What is Postman Newman?
Postman Newman is a CLI (command line interface) tool that can be used to run collections (Collection) and environments (Environment) in Postman for automated testing. It is Postman's command-line Collection Runner, capable of running Postman collections directly from the command line. Newman can be used to test the function, performance, reliability and security of the API, and at the same time, the test results can be output into reports in various formats, such as HTML, JSON , JUnit, etc., to facilitate developers to locate and analyze problems.
The role of Postman Newman
With automated testing by Postman Newman, developers can quickly get feedback on API performance after code changes and ensure code stability. Newman can be integrated with CI (Continuous Integration) and if any changes are pushed, CI will run the Postman collection together with Newman to ensure the stability of the environment. Various aspects of APIs such as functionality, performance, reliability, and security can also be tested using Newman to help developers identify and resolve issues.
How to use Postman Newman?
Here are the steps to test with Postman Newman:
Step 1: Install Node.js
Newman is built on Node.js, so Node.js needs to be installed before using Newman. Make sure you have Node.js installed, Node.js v4 or higher is recommended.
Step 2: Install Newman globally
Use npm to install Newman globally, you can install it with the following command:
npm install -g newman
Step 3: Export collections or environment variables in JSON format
Use Postman to export Collection or environment variables to files in JSON format, which are required when using Newman. Files can be exported by following these steps:
- Collection: Select the Collection to be exported, click the "Export" button in the upper right corner, select "Collection v2.1", and save it as a JSON file.
- Environment variable: Select the environment variable to be exported, click the "Export" button in the upper right corner, select "Environment", and save it as a JSON file.
Step 4: Run the tests with Newman
To run the test suite with Newman, use the following command:
newman run <collection.json> -e <environment.json> -r <reporter> --reporter-<option> <value>
where collection.json
is the path to the collection file, environment.json
is the path to the environment variable file, reporter
is the report format, and --reporter-[option] [value]
is the report option and value.
Step Five: Export the Report
Newman provides reports in three formats, including CLI, JSON and HTML formats, which can be -r
specified by parameters:
- CLI format: basic format, default report format displayed in cmd;
- JSON format: If you
-r json
specify to export the corresponding file, you need to--reporter-json-export
specify the storage path of the exported file. The content of the file is similar to the content of export result in Postman Runner, and it is not recommended to use it. - HTML format: Use
-r html
the specified export corresponding file, you need to--reporter-html-export
specify the storage path of the exported file. Installation ( ) is required before usenpm -g install newman-reporter-html
and is recommended.
For example, you can run sample-collection.json
the test cases in the collection and generate an report.html
HTML report named . The following commands can be used:
newman run sample-collection.json -e environment.json -r html --reporter-html-export report.html
This will generate an HTML report file named report.html
.
The above are the basic steps for testing with Postman Newman. If you need more advanced testing, you can check out Newman's documentation and examples for additional features and options.
Knowledge expansion:
Learn more about Postman related usage skills: