Java code coverage tools explained

Java code coverage tools are of two types: first, tools that add statements to the Java source code and require its recompilation. Second, tools that instrument the bytecode, either before or during execution. The goal is to find out which parts of the code are tested by registering the lines of code executed when running a test.

JaCoCo

JaCoCo
Developer:Marc Hoffmann, Brock Janiczak, Evgeny Mandrikov, Mirko Friedenhagen
Latest Release Version:0.8.12
Genre:Code coverage
License:EPL

JaCoCo is an open-source toolkit for measuring and reporting Java code coverage. JaCoCo is distributed under the terms of the Eclipse Public License. It was developed as a replacement for EMMA,[1] under the umbrella of the EclEmma plug-in for Eclipse.

Features

JaCoCo offers instructions, line and branch coverage.

In contrast to Atlassian Clover and OpenClover, which require instrumenting the source code, JaCoCo can instrument Java bytecode using two different approaches:

And can be configured to store the collected data in a file, or send it via TCP. Files from multiple runs or code parts can be merged easily.[3] Unlike Cobertura and EMMA it fully supports Java 7, Java 8,[4] Java 9, Java 10, Java 11, Java 12, Java 13, Java 14, Java 15, Java 16, Java 17, Java 18, Java 19 and Java 20.

Tools using or including JaCoCo

JCov

JCov
Developer:Leonid Arbouzov, Alexander Petrov, Vladimir Generalov, Serguei Chukhontsev, Oleg Uliankin, Gregory Steuck, Pavel Ozhdikhin, Konstantin Bobrovsky, Robert Field, Alexander Kuzmin, Leonid Mesnik, Sergey Borodin, Andrey Titov, Dmitry Fazunenko, Alexey Fedorchenko, Leonid Kuskov
Latest Release Version:3.0
Genre:Code coverage
License:GPL v2 (with the Classpath Exception)

JCov is the tool which has been developed and used with Sun JDK (and later Oracle JDK) from the very beginning of Java: from the version 1.1. JCov is capable of measuring and reporting Java code coverage. JCov is distributed under the terms of the GNU General Public License (version 2, with the Classpath Exception). JCov has become open-source as a part of OpenJDK code tools project in 2014.

Features

JCov is capable of reporting the following types of code coverage:

JCov implements two different ways to save the collected data:

JCov works by instrumenting Java bytecode using two different approaches:

JCov has a few more distinctive features which include, but are not limited to:

Tools using JCov

OpenClover

OpenClover
Developer:Marek Parfianowicz, Grzegorz Lewandowski
Latest Release Version:4.5.2
Genre:Code coverage
License:Apache License 2.0

OpenClover is a free and open-source successor of Atlassian Clover, created as a fork from the Clover code base published by Atlassian in 2017. It contains all features of the original Clover (the server edition). The OpenClover project is led by developers who maintained Clover in years 2012–2017.[13]

OpenClover uses source code instrumentation technique and handles Java, Groovy and AspectJ languages. Some of its features include: fine control over scope of coverage measurement, test optimisation and sophisticated reports.

OpenClover integrates with Ant, Maven, Gradle, Grails, Eclipse, IntelliJ IDEA, Bamboo, Jenkins, Hudson, Griffon, SonarQube and AspectJ.

IntelliJ IDEA Code Coverage Agent

IntelliJ IDEA Code Coverage Agent
Developer:JetBrains
Genre:Code coverage
License:Apache 2.0

IntelliJ IDEA Code Coverage Agent is a code coverage tool integrated in IntelliJ IDEA IDE and TeamCity CI server. It supports branch coverage and per-test coverage tracking.

Testwell CTC++ for Java

Testwell CTC++
Developer:Verifysoft Technology
Latest Release Version:8.0.
Genre:Code coverage
License:Proprietary

Testwell CTC++ is a code coverage tool for C, C++, Java and C#. The development of this tool started in 1989 at Testwell in Finland. Since 2013 support and development has been continued by Verifysoft Technology, a company from Offenburg, Germany. Testwell CTC++ analyses for all code coverage levels up to Modified condition/decision coverage and Multicondition Coverage.[14] The tool works with all compilers.[15]

Notable historic tools

Clover

Clover
Developer:Atlassian
Latest Release Version:4.1.2
Genre:Code coverage
License:Apache 2.0

Clover is a Java code coverage analysis utility bought and further developed by Atlassian. In April 2017 Atlassian announced end-of-life of Clover and at the same time open-sourced it under Apache 2.0 license.

Clover uses a source code instrumentation technique (as opposed to Cobertura and JaCoCo, which use byte code instrumentation), which has its advantages (such as an ability to collect code metrics) and disadvantages (re-compilation of sources is necessary).[16] Some of its features include historical reporting, huge control over the coverage gathering process, command line toolset and API for legacy integration and more.

Clover also allows testing time to be reduced by only running the tests that cover the application code that was modified since the previous build. This is called Test Optimization[17] and can lead to huge drops in the amount of time spent waiting for automated tests to complete.

Clover comes with a number of integrations both developed by Atlassian (Ant, Maven, Grails, Eclipse, IDEA, Bamboo) and by open source community (Gradle, Griffon, Jenkins, Hudson, Sonar).

In April 2017, Atlassian announced that they would no longer release new versions of Clover after version 4.1.2, and its code was made available as open-source software hosted on Bitbucket.[18] [19]

Cobertura

Cobertura
Developer:Steven Christou
Latest Release Version:2.1.1
Genre:Code coverage
License:GPL 2.0

Cobertura is an open-source tool for measuring code coverage. It does so by instrumenting the byte code. It was the predecessor to JaCoCo.

EMMA

EMMA
Developer:Vlad Roubtsov
Latest Release Version:2.1
Genre:Code coverage
License:Common Public License 1.0

EMMA is an open-source toolkit for measuring and reporting Java code coverage. EMMA is distributed under the terms of Common Public License v1.0.

EMMA is not currently under active development; the last stable release took place in mid-2005. As replacement, JaCoCo was developed.[20] EMMA works by wrapping each line of code and each condition with a flag, which is set when that line is executed.[21]

Features

Serenity

Serenity
Developer:Michael Couck
Latest Release Version:1.0
Genre:Code coverage
License:Apache Software License version 2.0

Serenity is an open-source tool creating better-automated software acceptance tests in less time. It and measures and reports Java code coverage. It also generates easy-to-understand reports that describe what the application does and how it works, including which tests were run and what requirements were met. It works with Selenium WebDriver, Appium, and BDD tools.

Major code metrics such as cyclometric complexity, stability, abstractness, and distance from main are measured. The report data is persisted to an object database and made available via Jenkins/Hudson. The interface visually replicates the Eclipse IDE interface.

Serenity dynamically enhances the byte code, making a post-compile step unnecessary. Ant and Maven projects are supported. Configuration is done in xml, an Ant example would be:

And a Maven configuration example would be:-Dincluded.packages=your.package.name.here-Dincluded.adapters=coverage,complexity,dependency-javaagent:serenity/serenity.jar -Xms512m -Xmx1024m $ $

For a full example of a configuration please refer to the Jenkins wiki at https://wiki.jenkins-ci.org/display/JENKINS/Serenity+Plugin.

Jenkins slaves as well as Maven multi module projects are supported.

Citations

External links

Notes and References

  1. http://www.eclemma.org/jacoco/trunk/doc/mission.html JaCoCo Mission
  2. Web site: Code Coverage Tools (JaCoCo, Cobertura, Emma) Comparison in Sonar. Patroklos Papapetrou. 19 December 2012 . Only Software matters . 3 March 2013.
  3. Web site: Measure Coverage by Integration Tests with Sonar – Updated . 2013-03-01 . 2013-02-23 . https://web.archive.org/web/20130223183723/http://www.sonarsource.org/measure-coverage-by-integration-tests-with-sonar-updated/ . dead .
  4. Web site: Code Coverage . https://archive.today/20130426184708/http://www.jetbrains.com/idea/webhelp/code-coverage-2.html . dead . 26 April 2013 . IntelliJ IDEA 12.0 Web Help . JetBrains . 3 March 2013 .
  5. http://www.eclemma.org/ EclEmma, Eclipse code coverage plugin
  6. https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin Jenkins JaCoCo Plugin
  7. Web site: NetBeans JaCoCo support . 2013-02-22 . 2012-05-31 . https://web.archive.org/web/20120531150325/http://wiki.netbeans.org/MavenCodeCoverage#Using_JaCoCo . dead .
  8. http://www.gradle.org/docs/current/userguide/jacoco_plugin.html Gradle JaCoCo Plugin
  9. https://www.jacoco.org/jacoco/trunk/doc/maven.html Maven JaCoCo Plugin
  10. https://blogs.msdn.microsoft.com/devops/2015/11/24/testing-java-applications-with-visual-studio-team-services/ JaCoCo integration in Visual Studio Team Services
  11. https://confluence.jetbrains.com/display/TCD10/JaCoCo JaCoCo integration in TeamCity
  12. https://github.com/STAMP-project/dspot#test-selectors--s----test-criterion Jacoco is used by STAMP DSpot tool as a test selector (seen as a fitness) to select tests that increase the coverage and have unique executed path
  13. http://openclover.org/about-us OpenClover - About us
  14. http://www.verifysoft.com/en_ctcpp_all_code_coverage_levels.html Testwell CTC++ supports all coverage levels
  15. http://www.verifysoft.com/en_code_coverage_all_compilers.html Testwell CTC++ supports all compilers
  16. https://confluence.atlassian.com/pages/viewpage.action?pageId=79986998 "Why does Clover use source code instrumentation?"
  17. https://www.atlassian.com/blog/archives/stop_testing_so_much Test Optimization
  18. Web site: Atlassian Clover is now open source. atlassian.com. 11 April 2017.
  19. Web site: atlassian/clover. bitbucket.org.
  20. http://sourceforge.net/projects/emma/files/ EMMA code coverage files on SourceForge.net
  21. https://books.google.com/books?id=L7d0LNpSrRwC&dq=EMMA+%28code+coverage+tool%29&pg=PA289 Expert Spring MVC and Web Flow; By Seth Ladd, Darren Davison, Steven Devijver, Colin Yates, p. 289