Generate merged code coverage report for JUnit and BDD tests in Sonar using Jacoco

Generate merged code coverage report for JUnit and BDD tests in Sonar using Jacoco

This tutorial guides you on how to generate generate merged code coverage report for JUnit and BDD tests in Sonar using Jacoco in Jenkins pipeline. If you wanted to achieve the Code Coverage criteria (%) with both Junit and BDD tests that was set in the pipeline as gating element to generate builds for deployments.

Generate merged code coverage report for JUnit and BDD tests in Sonar using Jacoco

In our previous article we had seen how to generate code coverage report for JUnit tests using Jacoco Maven Plugin. There would be a situation where developer will not be able to cover BDD cucumber tests for all the code that is written. Hence, you may want to cover unit tests for the remaining code using Junit.

In this article let’s learn approach to solve this, on how to generate combined code coverage report for both JUnit and BDD tests.Generate merged code coverage report for JUnit and BDD tests in Sonar using JacocoBelow are the step by step procedure that you need to work on in order to generate combined code coverage report in Jenkins pipeline.

1) Write Junit tests for the code, for which BDD tests can’t be written.

2) Then in your Jenkins pipeline you need to include stage for JUnit analysis and make sure it produce “jacaco.exec” coverage file.

3) Also, make sure that BDD acceptance tests too generating “exec” coverage file. Once your application code is running locally on Jenkins pipeline with Jacoco Java Agent enabled, then BDD tests will run and Jacoco agent will start recording the coverage.

4) Next, you need to combine both “exec” coverage files using Jacoco merge command as shown below.

java -jar jacococli.jar merge [<execfiles> ...] --destfile <path> [--help] [--quiet]

For example,

java -jar jacococli.jar merge first.exec second.exec --destfile finalmerged.exec

5) After, you run the above command, it would give the combined code coverage for both JUnit and BDD tests.

6) Finally, you can convert finalmerged.exec to xml report and publish it in Sonar server.

That’s it. Now you can have combined code coverage analysis for both JUnit and BDD tests in Sonar Server.

Hope it helped 🙂

Also See:

References

 

guest
0 Comments
Inline Feedbacks
View all comments