How to Automate to show API-Test Results with Java for GitLab&Slack
Hi everyone 👋🏼. In this article, I will try to explain the process that, how we prepared an “automated API test results” project using Java for each API Test project that uses GitLab CI for Seller Base Team in Trendyol. Before we begin, I want to share some information about my team.
The Seller Base team’s purpose is to manage sellers’ information such as creating domestic and international marketplace sellers. As the Seller Base team, we have around 10 acceptance test projects on GitLab. We use acceptance tests to check the projects that comply with our test criterias before we deploy to the production. Managing to get an idea for all of these test projects’ reports is tedious work to look at each of them one by one with Allure Report. So, we decided to get rid of manual labor.
First, we have been checking every Test project, manually counting and writing those tests in Excel to keep track of the API Test count for each week. And we decided to change it to have more time for other stuff.
Second, we pivoted our perspective from writing Excel to Slack. It would be more convenient to read from a Slack channel rather than dribbling in an Excel file to find what we want, why we chose Slack over Excel because Slack has more advantageous ways to integrate with GitLab and we use it for our daily work.
At the beginning of what we had done, we decided to write a Java program that would allow us to get into a single pipeline job that succeeded.
To access the acceptance tests pipeline that succeeded, we needed to get the test results from a project on GitLab. After a quick research, We found that we could get test results from a file that is named TEST-TestSuite.xml that is created by the maven-surefire-plugin.
After we added the JUnit reports step, it worked like a charm. It looked like the below in the pipeline.
The trick will be the Java program that connects to the GitLab APIs and fetch the results for us. So Let’s start with the configuration of our Acceptance Test Result project.
Configurations
These are the configurations for our “Acceptance Test Results” project.
When we get a response that contains JobModel that only holds the pipelineId. With that, we now request for report summary to get those test results.
We mapped the response with the Gson library to a Modal that’s some handy fields later. Below we can see the final request and the TestResultModel content. To understand the writeLog method briefly, it writes TestResultModel in a way that we need to show users the tests’ count, success, failed, and the time we executed the result project date.
The writeLog method
The writeLog method is meant to write the response to a file and to the console. Your desire can alter this method. The “StringUtils.threeDot” method is to make the test name shortened and place three dots at the end.
The important thing here is, you need to configure the log4j to write a log file. Here is the log4j.xml to give you an idea.
After configuring this file, put it in the resources folder to complete the log4j configuration.
Uploading the log file to The GitLab
Uploading to GitLab, collaborates with the slack integration. First, we need to look at gitlab-ci.yml.
To understand what is happening in the gitlab-ci.yml file, first, let’s look at the variables we provided.
The most significant variables are the PROJECT_IDS and the PROJECT_LOG_PATH to execute this script on GitLab. I created a main class so we can give such parameters with commas. You can change the structure of the way this project works. In the execute stage, we run a script that takes maven parameters.
To explain these parameters:
- -Dexec.arguments=”$PROJECT_IDS” => works with “exec-maven-plugin”. We need to provide projectIds taken from the GitLab for each project we want to show on Slack.
- -Dexec.cleanupDaemonThreads=false => Necessary to shut down even if there are DaemonThreads.
- -DGITLAB_API_TOKEN=${GITLAB_API_TOKEN} => we are using this property in the project so that we can request with the PRIVATE_TOKEN header in the Configuration section.
After the project runs in the GitLab environment, the artifact is uploaded to the server with the API Test Results.
Finally, the Slack
The slack configuration depends on for each user. The Information which I’ll give here is specifically tweaked for my needs.
The filePath is taken from the gitlab-ci.yml as a first parameter. This is for writing the log file to slack as a prompt.
The testCountUrl is to show the link of the log file we uploaded to GitLab servers.
After a long journey, we can see three projects’ test results in the slack channel that we’ve provided In the gitlab-ci.yml
You can see the photo below to look at the Test Count Url.
Summary
That’s all folks. I hope this article helps open up new windows in your mind. I tried to explain a problem that we faced. We devised a workaround to make our lives easier to keep track of the test results and whether our system is working properly by looking at those test results in one place. See you later 👋🏼 . . .