The goal of this project is to implement an Automation Testing for a fake online REST API called ReqRes
. We will use:
Jenkins
, a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software;Docker-in-Docker (dind)
to executeDocker
commands insideJenkins
;Postman API Client
, for testing API calls;Newman
, a command-line collection runner forPostman
.
On ivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
-
Open a terminal and make sure you are inside
jenkins-dind-postman-newman
root folder -
Execute the following command. I will run
Newman
docker container using thePostman
collection present inpostman
folder.docker run -t --rm --name newman -v $PWD/postman:/etc/newman \ postman/newman:6.1.1-alpine run ReqRes.postman_collection.json --globals ReqRes.postman_globals.json
In
postman
folder, there are two JSON files that configure some test cases and environment variables used to run them. You can edit them by usingPostman
.You should get as response:
ReqRes → Get List of Users GET https://reqres.in/api/users [200 OK, 1.92kB, 302ms] ✓ Status code is 200 ✓ Content-Type is present ✓ Response time is less than 1000ms ✓ Page Schema is valid ✓ Field values → Get Single User GET https://reqres.in/api/users/2 [200 OK, 1.22kB, 60ms] ✓ Status code is 200 ✓ Content-Type is present ✓ Response time is less than 1000ms ✓ User Schema is valid ✓ Field values → Get Nonexistent User GET https://reqres.in/api/users/20 [404 Not Found, 898B, 120ms] ✓ Status code is 404 ✓ Content-Type is present ✓ Response time is less than 1000ms → Post User POST https://reqres.in/api/users [201 Created, 929B, 117ms] ✓ Status code is 201 ✓ Content-Type is present ✓ Response time is less than 1000ms ✓ User Schema is valid → Put User PUT https://reqres.in/api/users/2 [200 OK, 970B, 117ms] ✓ Status code is 200 ✓ Content-Type is present ✓ Response time is less than 1000ms ✓ User Schema is valid → Delete User DELETE https://reqres.in/api/users/2 [204 No Content, 804B, 102ms] ✓ Status code is 204 ✓ Response time is less than 1000ms ┌─────────────────────────┬────────────────────┬───────────────────┐ │ │ executed │ failed │ ├─────────────────────────┼────────────────────┼───────────────────┤ │ iterations │ 1 │ 0 │ ├─────────────────────────┼────────────────────┼───────────────────┤ │ requests │ 6 │ 0 │ ├─────────────────────────┼────────────────────┼───────────────────┤ │ test-scripts │ 12 │ 0 │ ├─────────────────────────┼────────────────────┼───────────────────┤ │ prerequest-scripts │ 6 │ 0 │ ├─────────────────────────┼────────────────────┼───────────────────┤ │ assertions │ 23 │ 0 │ ├─────────────────────────┴────────────────────┴───────────────────┤ │ total run duration: 1149ms │ ├──────────────────────────────────────────────────────────────────┤ │ total data received: 1.43kB (approx) │ ├──────────────────────────────────────────────────────────────────┤ │ average response time: 136ms [min: 60ms, max: 302ms, s.d.: 76ms] │ └──────────────────────────────────────────────────────────────────┘
-
In a terminal, make sure you are inside
jenkins-dind-postman-newman
root folder -
Run the command below
docker compose up -d
Note 1: A new image called
jenkins-blueocean:2.440.2-jdk17
is build from theJenkins
base imagejenkins/jenkins:2.440.2-jdk17
. To rebuild this image you must usedocker compose build
ordocker compose up --build
Note 2: If you prefer to run
jenkins-docker
andjenkins
Docker containers usingdocker run
command instead, follow the steps described at jenkins.io website -
Wait a bit so that
jenkins
container isUp (healthy)
. To check it rundocker compose ps
-
In a terminal, get the
Jenkins
installation password by running the following commanddocker logs jenkins
You should see something similar to the sample below, where the password is informed
************************************************************* ************************************************************* ************************************************************* Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: 1b5964b8c7a648e584a994fbb293c05f This may also be found at: /var/jenkins_home/secrets/initialAdminPassword ************************************************************* ************************************************************* *************************************************************
-
Access
Jenkins
at http://localhost:8080 -
Inform the installation password and click
Continue
-
Select
Install suggested plugins
(default installation) and wait for it to complete -
On the
Create First Admin User
screen, create an account informing username, password, etc. Then, clickSave and Continue
button -
Keep the
Jenkins URL
as it's (it should behttp://localhost:8080/
), and clickSave and Finish
button -
Jenkins is ready! Click
Start using Jenkins
-
In the main
Jenkins
interface, clickNew item
menu on the left -
Enter a name, for instance,
rest-api-automation-testing
-
Select
Freestyle project
and clickOK
button -
In the next screen, go to
Build Steps
section. ClickAdd build step
and selectExecute shell
-
Set the content below to the
Command
fielddocker run --rm postman/newman:6.1.1-alpine \ run "https://raw.githubusercontent.com/ivangfr/postman-newman-jenkins/master/postman/ReqRes.postman_collection.json" \ --globals "https://raw.githubusercontent.com/ivangfr/postman-newman-jenkins/master/postman/ReqRes.postman_globals.json" \ --disable-unicode --color off
-
Click
Save
button -
Your Jenkins project is created. You should see something like
-
In order to run the project, click
Build Now
menu item on the leftNote: The test cases are a bit strict. They require that the response time must be below
1000
ms. So, depending on how fastReqRes
online REST API replies to you request, maybe some test cases will fail and, consequently, the build will fail. -
To see the execution results, click the red or green icons that appears inside
Build History
(section on the left) everytime you build theJenkins
project.
-
In a terminal, make sure you are inside
jenkins-dind-postman-newman
root folder -
To stop and remove docker-compose containers, network and volumes run
docker compose down -v
To remove the Docker image create by this project, go to a terminal and, jenkins-dind-postman-newman
root folder, run the following script
./remove-docker-images.sh