Java, Web and Mobile

Blog

Display the test coverage with GitLab

|
Continuous Integration

In the previous guides of this series, we have seen how to set up auto deployment and to auto publish documentation. As last step for a transparent project, we want to show the user the reached test coverage. In the following we walk you through the process compute the test coverage and show it within GitLab.

What you will achieve

A maven build which computes the test coverage and a badged to directly display the test coverage of the project.

What you will need

  • An account on a GitLab-Server (for example gitlab.com)
  • A sample program with initial configuration
  • Basic knowledge about the GitLab-CI
  • Git in Version 2.0.0 or later
  • Jdk 1.8
  • Maven 3.0+

Set up

You can use any maven project in combination with the following GitLab-CI configuration or a similar one. If you have no project on hand you can create one with spring initializer or use our sample program.

The following configuration defines two jobs. One for tagged commits and one for all other. In the case of non tagged commits, the app is compiled and tested. The job for tagged commits additionally deploys the result.

Providing automatic feedback on test coverage

First a plugin is added to compute the test coverage. JaCoCo is choosen but other possibilities are easily adaptable.

The Maven configuration for JaCoCo is straight forward:

The result are some HTML pages which could be published with GitLab Pages as well (see our post about automatically publish documentation for instructions). For getting the information of all branches in GitLab it is necessary to print it out to the console.
Because GitLab can only obtain the coverage ratio by parsing a console output. According to a regular expression the information can be provided by extracting it from the index.html of the JaCoCo report. It is done by using a combination of cat, grep, and sed.

Now we can filter out the line where Jacoco-Test-Coverage:x.y% is printed using a regex configuration in GitLab at Settings/Pipelines/Test coverage parsing.

By means of the following line Jacoco-Test-Coverage:x.y%is printed by using a regex configuration in GitLab at Settings/Pipelines/Test coverage parsing.

This allows to use test coverage badges. The links can be find in the same settings section.

Badges

Naturally other metrics can be used as well, as long they can be printed to the console.

Conclusion

We have seen how to add the computed test coverage as badges on the read me page of our
project.