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 executeDockercommands 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-newmanroot folder -
Execute the following command. I will run
Newmandocker container using thePostmancollection present inpostmanfolder.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.jsonIn
postmanfolder, 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-newmanroot folder -
Run the command below
docker compose up -dNote 1: A new image called
jenkins-blueocean:2.440.2-jdk17is build from theJenkinsbase imagejenkins/jenkins:2.440.2-jdk17. To rebuild this image you must usedocker compose buildordocker compose up --buildNote 2: If you prefer to run
jenkins-dockerandjenkinsDocker containers usingdocker runcommand instead, follow the steps described at jenkins.io website -
Wait a bit so that
jenkinscontainer isUp (healthy). To check it rundocker compose ps
-
In a terminal, get the
Jenkinsinstallation password by running the following commanddocker logs jenkinsYou 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
Jenkinsat 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 Userscreen, create an account informing username, password, etc. Then, clickSave and Continuebutton -
Keep the
Jenkins URLas it's (it should behttp://localhost:8080/), and clickSave and Finishbutton -
Jenkins is ready! Click
Start using Jenkins
-
In the main
Jenkinsinterface, clickNew itemmenu on the left -
Enter a name, for instance,
rest-api-automation-testing -
Select
Freestyle projectand clickOKbutton -
In the next screen, go to
Build Stepssection. ClickAdd build stepand selectExecute shell -
Set the content below to the
Commandfielddocker 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
Savebutton -
Your Jenkins project is created. You should see something like
-
In order to run the project, click
Build Nowmenu item on the leftNote: The test cases are a bit strict. They require that the response time must be below
1000ms. So, depending on how fastReqResonline 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 theJenkinsproject.
-
In a terminal, make sure you are inside
jenkins-dind-postman-newmanroot 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

