Run your ABAP Unit test in CI pipelines
In this blog we will take a look at how to run your ABAP Unit tests in CI pipeline when publishing from AbapGit.
There are some really cool stuff already in this area such as:
- Lars Hvam’s blog about running ABAP code in Githut actions without an ABAP stack
- Andreas Gautsch blog about his Eclipse plugin for ADT
- Felipe Silva blog about monitoring Abap packages in pipelines
The difference between Felipe’s blog and mine is that in this blog I will explain how to run the unit tests without relying on additional source code installed on the Abap server.
Why would you even want to consider this approach. You could just keep running your Unit tests like normal.
While this is certainly true, I don’t think you can test too often, so adding another layer of testing in a pipeline where you could also integrate other packages as well is pretty cool. Or possibly you could use this setup to potentially shift your development to a server off the common transport path (See Graham Robinson‘s blog for inspiration) to later integrate into the test system via a pipeline.
Let me explain my setup for this blog, everything is running in separate Docker containers.
If you want me to write a second blog on how to set up the gitlab stuff, let me know in the comments below.
For this example I’ve created a local package $TEST_UNIT2 with a single class in it called ZCL_ABAP_UNIT2
In this class I’ve got a simple method called test2
And a local test class
While this obviously isn’t a very elaborate scenario, it does the job for this blog 😉
When I execute this locally in VSCode by pressing CTRL + SHIFT + F11 or using the command palette and typing abapfs ‘run unit tests’ I can then see the test result in the test explorer window
Now let’s add this to a pipeline.
Firstly on my gitlab server I create a new repository.
Afterwards we copy the http link to be used in AbapGit
Now we clone the repository in AbapGit and then we commit and push the changes to the git repository
Now that we have pushed out code, it’s time to build the pipeline. Click the new file.
Select the .gitlab-ci.yml template and paste the following in.
image: node:latest # This folder is cached between builds # https://docs.gitlab.com/ee/ci/yaml/index.html#cache cache: paths: - node_modules/ abap_unit: script: - npm install -g abap_test_runner_cli - abaptr
In the pipeline we will install an NPM package I have created. This will execute the unit tests and give us the output.
Here I use the package. If you want to use the class, then replace the ABAP_PACKAGE with ABAP_CLASS.
Now let’s save this and try to run our pipeline.
We can see under the Pipeline –> CI that our pipeline has executed correctly
And checking the job log it’s passed all unit tests as expected
Now let’s change the source code to make the test fail.
After committing the code to Gitlab via AbapGit we see that our pipeline is now failing
Checking the details of the job we get the expected fail on the unit test. 1 is actually different to 2. Who knew!
Alright,not it’s time to get creative. How about we let the pipeline create a gitlab issue directly on a failed test.
Check this out
# This file is a template, and might need editing before it works on your project. # To contribute improvements to CI/CD templates, please follow the Development guide at: # https://docs.gitlab.com/ee/development/cicd/templates.html # This specific template is located at: # https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Nodejs.gitlab-ci.yml # Official framework image. Look for the different tagged releases at: # https://hub.docker.com/r/library/node/tags/ image: node:latest stages: - abap_unit - create_issue abap_unit: stage: abap_unit script: - npm install -g abap_test_runner_cli - echo "$( abaptr --skip_image true )" >> test.txt artifacts: paths: - test.txt create_issue: stage: create_issue script: - apt-get update - apt-get install python3 -y - |- if [[ $( grep -q 'Error' test.txt && echo true || echo false ) == true ]]; then echo "Creating issue" curl --request POST --header "PRIVATE-TOKEN: $private_token" "https://gitlab.agilux.com.au:44388/api/v4/projects/$CI_PROJECT_ID/issues?title=Unit%20test%20failed&description=$(cat test.txt | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))")" exit 1 else echo "success" exit 0 fi dependencies: - abap_unit
Alright let’s go through this step by step.
The stages section of the yml file is to indicate that the jobs are to be run sequentially and not in parallel