How to Get Command Line Integration with Newman in Postman
Postman is a tool for sending requests to API endpoints. It also provides lots of useful functionalities on top of the main HTTP requests like API documentation, monitoring, simulating a mock server, etc. If you want to learn how to use Postman, check out this blog post “How to Use Postman to Manage and Execute Your APIs”.
Newman is a nice part of Postman, and it makes Postman more powerful. Newman is a command line executor for Postman tests, enabling you to run a Postman collection from the command line. By using Newman, we can integrate API test automation to Continuous Integration tools.
In this blog post I will show you how to use Newman. To do that, I will test a sample API from Postman, which is a simple GET request. Now let’s see what we can achieve here.
First of all we need to create our first collection in Postman. In this collection we should include all the API tests we created for this sample API. This is what it looks like:
Here are the tests:
With a slight change, it looks like this:
Now we can make sure that our API GET request is responding with the 401 HTTP code. The power behind this is JS, making is quite easy to write many tests for specific coverage, including schema validation for a JSON response body, and much more.
Ok, let’s say we finished collecting our tests together, what’s next? We can easily rerun them as many times as we need. But wait, that’s manual, isn’t it? Yes, we need to test automatically, because in the 21st Century manual testing is… no, I will not comment on that.
As a first step, we need to export our Collection to a JSON file in Postman format. To do that, highlight your collection and then click on “Export”.
Now, save the file in any prefered location and name it as you like. I will do it in ~/Projects/Postman/getPostmanAuth.json.
Now we need to install Newman. Simply follow the steps from here.
When you’re done open the command line and navigate to the path of the just exported json collection file.
For me the command will look like this:
Here, the magic begins! Run the Newman command:
(Replace “getPostmanAuth” with your JSON file name.)
If you did everything correctly you will see this output:
Cool huh? You can see the reports in your command line!
Ok, agreed, but stop, this report in STDOUT is fine enough, but we are in the modern world and we need to have cooler reports like HTML or JUnit formatted XML. Then, we will be able to put our new shiny Newman into CI systems, like Jenkins.
Let's try, here is the command:
Then, you will see a new folder next to your Newman JSON file. Inside it, you will find the same data in a nicer report. In my case: newman-run-report-2017-02-12-14-48-36-139-0.html. Try to open it in a browser, e.g. Google Chrome:
This is already ready to be included in Jenkins, or any other CI system.
You can also use JSON and JUNIT/XML reporters in Jenkins. To do that, go to Post-build Actions on Jenkins in one of your jobs or by creating a new job:
To generate the JUnit XML that will be put in the “Test report XMLs” field, run this command:
The result will be in the same folder with all the other reports, with a name like this: newman-run-report-2017-02-12-14-54-40-145-0.xml. It will already be properly formatted into a JUnit publisher ready file:
Now run the test and view the results:
You can also run JSON reports on monitoring systems like Elastic Search and Kibana, by running:
Thanks for reading! If there are any questions, please ask us in the comments section.
Get your API testing started with BlazeMeter. To try us out, just put your URL and assertion in the box below and your test will start in minutes.