Introduction
Jenkins is an open source automation server that is used to automate the process of building, testing, checking code coverage, deployment, checking for security scan, etc. Based on our requirements, we can add script to a job. This will automate the process. In this article, we will look into one part of this: how we can test API using Jenkins.
To install Jenkins, refer to the URL: https://www.jenkins.io/doc/book/installing/ and based on your OS, follow the instruction suggested. I am demonstrating using Mac OS with Jenkins-its.
After installing, in the terminal, use the below command to start the Jenkins server in local:
brew services start jenkins-lts
Jenkins will start the server at 8080, and will redirect to the home page. Below is an image of home screen:
Use the username: admin
To get the password, use command: cat /Users/<system_user_id>/.jenkins/secrets/initialAdminPassword
After a successful log in, it will redirect you to the Jenkins dashboard. Click on New Item.
And enter item name and select pipeline for testing purpose. Click OK.
Next, you will redirect to configure page, where we can select Pipeline:
In the pipeline, will use Groovy script like below to test two APIs like those below. Click Save.
node {
def apiHostedUrl = "http://localhost:8000"
stage('TestWelcome') {
URL url = new URL(apiHostedUrl + "/welcome/abc");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = "";
while (line != null) {
sb.append(line);
line = br.readLine();
}
br.close();
println sb.toString();
assert sb.toString() == "hello abc"
println "Final status of welcome = " + (sb.toString() == "hello abc").toString();
http.disconnect();
}
stage('TestSquare') {
URL url = new URL(apiHostedUrl + "/Square/5");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = "";
while (line != null) {
sb.append(line);
line = br.readLine();
}
br.close();
println sb.toString();
assert sb.toString() == "25"
println "Final status of TestSquare = " + (sb.toString() == "25").toString();
http.disconnect();
}
}
We can see that the above script is written to test two APIs with two stages, i.e., TestWelcome and TestSquare.
To test this as mentioned in the script, host an API at local. This should be listening at 8000: http://127.0.0.1:8000/
Implement two simple API welcome and squaring numbers.
For instance, I have used Python with flask framework. The source code is attached to this article. You can download and execute the below command to run it.
Open the project in VS code. In the terminal execute:
- pipenv shell
- pipenv sync
- python sample_api.py
It will host API at: http://127.0.0.1:8000/
After this, go back to the Jenkins server and click on Build Now. This will execute and show results for both TestWelcome and TestSquare.
To check the logs, click on #8 shown in the image below:
Following that, click Console Output.
Summary
In this article, we understood the usage of Jenkins for testing API using groovy script.