Run Postman tests with Newman in Gitlab CI
In this article, let me show you how to run Postman
tests in Gitlab CI/CD
pipeline. Basically, we’re working with api and testing on web or client but in order to run Postman tests while pipeline progress, we need to use Newman
which a runner to run and test a Postman collection from the command-line.
1. Generate an API Key
First, we have to get an Postman API Key, because it’s needed to get collection and environment url via Postman API for running tests with Newman.
- Go to web > Choose workspace > Tab ‘Integrations’
- Tab ‘Browse Integraions’ > Choose ‘Postman API’ > Click ‘+ Get API Key’
2. Get the Collection URL
It takes two steps to get the collection url to test via Postman API with the API key. One step is that fetch a list of your collection and get a uid
of the collection to test in the list. Last step is that get the collection url with the uid
from previous step.
2.1 Fetch a collection list & Get a `uid` of the collection
https://api.getpostman.com/collections?apikey=${apiKey}
2.2 Get the collection url
https://api.getpostman.com/collections/${c_uid}?apikey=${apiKey}
3. Get the Environment URL
It’s the same as above getting the collection url.
3.1 Fetch a environment list & Get a ‘uid’ of the environment
https://api.getpostman.com/environments?apikey=${apiKey}
3.2 Get the environment url
https://api.getpostman.com/environments/${e_uid}?apikey=${apiKey}
4. .gitlab-ci.yml
Last, to run Postman test in Gitlab CI/CD, let’s add a job which call Newman ‘run’ command in the pipeline to the .gitlab-ci.yml
. There are two cases whether based on docker or not. If without docker, it needs to install Newman in before_script
. Otherwise, wojciechzurek/newman-ci
docker image helps you not have to the install it.
Without docker
With docker
Ref.
- Command line integration with Newman — Postman : https://www.getpostman.com/docs/v6/postman/collection_runs/command_line_integration_with_newman
- Newman : https://github.com/postmanlabs/newman
- Newman-ci : https://github.com/wojciech-zurek/newman-ci