Automating API Testing with Newman
API testing is a crucial part of modern software development. It ensures that APIs function as expected, providing the necessary data and responses for different client requests. Postman is a popular tool for testing APIs manually, but automating these tests is essential for continuous integration and deployment (CI/CD) pipelines. This is where Newman comes into play.
What is Newman?
Newman is a command-line collection runner for Postman. It allows you to run and test Postman collections directly from the command line, making it ideal for automation and integration into CI/CD workflows. With Newman, you can execute a series of API requests, validate responses, and generate detailed reports.
Installing Newman
Before you can use Newman, you need to have Node.js installed on your system. If you don’t have Node.js installed, you can download it from the official website.
Once Node.js is installed, you can install Newman using npm (Node Package Manager) with the following command:
npm install -g newman
This command installs Newman globally, making it accessible from any directory on your system.
Running a Postman Collection with Newman
To run a Postman collection with Newman, you need an exported Postman collection file (in JSON format). You can export a collection from Postman by selecting the collection, clicking on the “…” menu, and choosing “Export”.
With your exported collection file ready, you can run it using Newman with the following command:
newman run {{path/to/your/postman_collection.json}} -e {{path/to/env.json}}
Generating Reports
Newman can generate various types of reports to help you analyze the results of your API tests. By default, Newman outputs results to the console, but you can also generate HTML, JSON, or JUnit reports.
To generate an HTML report, use the -r
flag with the html
reporter:
Install html report
npm install -g newman-reporter-html
newman run {{path/to/your/postman_collection.json}} -e {{path/to/env.json}} -r html
This command generates an HTML report in the newman
directory. You can specify a different directory or file name using the --reporter-html-export
option:
newman run {{path/to/your/postman_collection.json}} -e {{path/to/env.json}} -r html --reporter-html-export path/to/report.html
Integrating Newman with CI/CD
One of the main benefits of using Newman is its seamless integration with CI/CD pipelines. Most CI/CD tools, such as Jenkins, GitLab CI, and Travis CI, support running shell commands, making it easy to incorporate Newman into your workflow.
Here’s an example of how you might integrate Newman with a Jenkins pipeline:
pipeline {
agent any
stages {
stage('Install Newman') {
steps {
sh 'npm install -g newman'
}
}
stage('Run API Tests') {
steps {
sh 'newman run path/to/your/postman_collection.json -r html,junit --reporter-html-export reports/results.html--reporter-junit-export newman/RESULTS.xml'
}
}
stage('Publish HTML Report') {
steps {
publishHTML(target: [
allowMissing: true,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: 'reports',
reportFiles: '*.html',
reportName: 'Newman Report',
])
}
}
}
post {
always {
script {
// Archive the HTML report directly as an artifact
archiveArtifacts artifacts: 'reports/results.html','newman/RESULTS.xml', allowEmptyArchive: true
}
}
}
This Jenkinsfile installs Newman, runs the Postman collection, generates a JUnit report, and publishes the test results.
Conclusion
Newman is a powerful tool for automating API tests, making it easier to ensure the reliability and functionality of your APIs. By integrating Newman into your CI/CD pipeline, you can run tests automatically with each build, catch issues early, and maintain a high level of quality in your software development process.
With its ease of use, flexibility, and robust reporting capabilities, Newman is an essential tool for any development team looking to streamline their API testing and automation efforts.