Running Postman in Jenkins

Table of contents
Reading Time: 3 minutes

There are many tutorials on how to run Postman tests in Jenkins, but they leave out important details such as how to safely pass in credentials into your Postman tests. I assume that you already have some knowledge of Postman and Jenkins.

In order to run Postman in Jenkins you must be able to run Postman tests on the command line. Newman is used to run Postman on the command line. To install newman execute the following command

npm install -g newman

You can now run Postman with the following command

newman run <collection>

Where <collection> is a path to a file containing a collection in Json format. If you have a collection you can export it. You can use additional options. For example to ignore certificates use -k

newman run <collection> -k

To pass in variables to your tests use –env-var

newman run <collection> -k --env-var <key1>=<val1> --env-var <key2>=<val2>

We can now add a stage to a Groovy script to run Postman tests.

stage('Postman') {
    steps {
        sh "newman run <collection> -k <key1>=<val1> --env-var <key2>=<val2>"

What if we want to pass in credentials to our tests? In this case it is best to choose a freestyle project


Add a build step of type “Execute Groovy script”

Add the following to your Groovy script.

This does not yet protect our credentials. Go to the “Build Environment” section. Check “Use secret text(s) or file(s)”. Go to the “Bindings” section and choose “Username and password (separated)”.

Add credentials if you don’t have them in your Jenkins server already.

We can now modify our Groovy script to use the credentials.

We can specify source code management so that we can get our collection from a git repository. We can specify build triggers so that our tests are run periodically or after another project is built. We can add post-build actions such as sending email alerts if the Postman tests fail.