DEV Community

Zafri Zulkipli
Zafri Zulkipli

Posted on

Automated API Testing with Jenkins and Postman/Newman

Let me start off by saying that this is purely experimental, I am not using this strategy in my workplace nor any production environment. This is me, spending my lazy Sunday afternoon playing around with API testing automation.

I got this idea to play around with Jenkins/Postman after my workplace is looking for an API testing automation tool that would run periodically and notify us immediately when something fails.

I knew Postman is perfect for running multiple API tests with it's Runner and pair it with Newman I can execute collections in headless state.

I stumbled upon Jenkins while googling away how to automate the process of running Postman collections. So now I have the means to test and automate. Below is my step-by-step setup for running Postman + Newman + Jenkins.

My folder structure

automated-test
├── docker-compose.yml
├── Dockerfile
├── postman_collections
├── .. # some other Jenkins related stuff that is not in the scope of this post

I begin by extending the official Jenkins docker image to include Newman.

Dockerfile

FROM jenkins/jenkins:lts

USER root

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    # install nodejs
    && curl -sL https://deb.nodesource.com/setup_10.x | bash - \
    && apt-get install -y apt-utils \
    && apt-get install -y nodejs \
    && apt-get install -y build-essential \
    && apt-get install -y inotify-tools \
    # install newman
    && npm install -g newman

USER jenkins

Then build using docker-compose (since I'm lazy to type a long list of docker command just to start Jenkin server)

docker-compose.yml

version: "3"

services:
  app:
    build: .
    volumes:
      - .:/var/jenkins_home
    ports:
      - "8080:8080"
      - "50000:50000"

After that, a simple docker-compose up will suffice.

So I got my Jenkins server up and running, now I need to export my postman collections. Below is a simple API test that I made. It calls an endpoint and assert the HTTP response status is 200, pretty simple right?

I exported the collection to ~/automated-test/postman_collections/collection.json

Now, I need to create a new project in Jenkins dashboard.

The configs worth noting is

  1. Interval to run the test (I set it to run every 5min)
    Build Triggers > Build periodically > */5 * * * *

  2. Actual test execution for the postman collections
    Build > Add build step > Execute shell > newman run /var/jenkins_home/postman_collections/collection.json

Once saved, Jenkins will execute your postman collections every 5min.

And done! Of course using Jenkins just to test for Postman API collections is a bit overkill, but it does the job. If you have any other automation tool that you find suitable, do let me know since my workplace is still looking for a suitable API testing automation.

Top comments (0)