Are you using Postman to run your API tests? And are you familiar with Postman Collection runner? Today I’d like to share with you my experience of using Newman, a command line collection runner for Postman. The benefits?  The availability to integrate with your CI.

Let’s create an API test and run it on Postman.

For the test I will use get method, to obtain list of users attacking next endpoint:

https://reqres.in/api/users?page=2

newman postman

Let’s write some tests for this:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    pm.expect(pm.response.text()).to.include("email");
});

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

Then, it’s time to run these tests from Collection runner:

From here, the next step which we are going to do is exporting the collection as a json.

After it’s successfully exported, we need to install newman. It can be easily installed from npm.

npm install -g newman

And to run it just do:

newman run mycollection.json

There is also the possibility to pass the collection as a url and passing environment variable:

newman run https://www.example.com/collections/test -e dev_environment.json

Providing also some set of options. Like -n flag for iteration-count number, -g for globals file, –folder to run from specific folder name.

And finally, let’s integrate out postman tests with CI. In this example I’ll be using Gitlab CI. Also, I will install reporter and generate an html file which will upload to gitlab artifacts.

And the report will look like:

That’s all folks! Happy Testing!