Home | History | Annotate | Download | only in doc
      1 <?xml version="1.0" encoding="UTF-8" ?>
      2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
      4 <head>
      5   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      6   <link rel="stylesheet" href=".resources/doc.css" charset="UTF-8" type="text/css" />
      7   <link rel="shortcut icon" href=".resources/report.gif" type="image/gif" />
      8   <title>JaCoCo - Coverage Counter</title>
      9 </head>
     10 <body>
     11 
     12 <div class="breadcrumb">
     13   <a href="../index.html" class="el_report">JaCoCo</a> &gt;
     14   <a href="index.html" class="el_group">Documentation</a> &gt;
     15   <span class="el_source">Coverage Counters</span>
     16 </div>
     17 <div id="content"> 
     18 
     19 <h1>Coverage Counters</h1>
     20 
     21 <p>
     22   JaCoCo uses a set of different counters to calculate coverage metrics. All
     23   these counters are derived from information contained in Java class files
     24   which basically are Java byte code instructions and debug information 
     25   optionally embedded in class files. This approach allows efficient on-the-fly
     26   instrumentation and analysis of applications even when no source code is
     27   available. In most cases the collected information can be mapped back to
     28   source code and visualized down to line level granularity. Anyhow there are
     29   limitations to this approach. The class files have to be compiled with debug
     30   information to calculate line level coverage and provide source highlighting.
     31   Not all Java language constructs can be directly compiled to corresponding
     32   byte code. In such cases the Java compiler creates so called <i>synthetic</i>
     33   code which sometimes results in unexpected code coverage results. 
     34 </p>
     35 
     36 <h2>Instructions (C0 Coverage)</h2>
     37 
     38 <p>
     39   The smallest unit JaCoCo counts are single Java byte code instructions.
     40   <i>Instruction coverage</i> provides information about the amount of code that
     41   has been executed or missed. This metric is completely independent from source
     42   formatting and always available, even in absence of debug information in the
     43   class files.
     44 </p>
     45 
     46 <h2>Branches (C1 Coverage)</h2>
     47 
     48 <p>
     49   JaCoCo also calculates <i>branch coverage</i> for all <code>if</code> and
     50   <code>switch</code> statements. This metric counts the total number of such
     51   branches in a method and determines the number of executed or missed branches.
     52   Branch coverage is always available, even in absence of debug information in
     53   the class files. Note that exception handling is not considered as branches
     54   in the context of this counter definition.
     55 </p>
     56 
     57 <p>
     58   If the class files haven been compiled with debug information decision points
     59   can be mapped to source lines and highlighted accordingly:
     60 </p>
     61 
     62 <ul>
     63   <li>No coverage: No branches in the line has been executed (red diamond)</li>
     64   <li>Partial coverage: Only a part of the branches in the line have been
     65       executed (yellow diamond)</li>
     66   <li>Full coverage: All branches in the line have been executed (green diamond)</li>
     67 </ul>
     68 
     69 <h2>Cyclomatic Complexity</h2>
     70 
     71 <p>
     72   JaCoCo also calculates cyclomatic complexity for each non-abstract method and
     73   summarizes complexity for classes, packages and groups. According to its
     74   definition by
     75   <a href="http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/title.htm">McCabe1996</a>
     76   cyclomatic complexity is the minimum number of paths that can, in (linear)
     77   combination, generate all possible paths through a method. Thus the
     78   complexity value can serve as an indication for the number of unit test cases
     79   to fully cover a certain piece of software. Complexity figures can always be
     80   calculated, even in absence of debug information in the class files.
     81 </p>
     82 
     83 <p>
     84   The formal definition of the cyclomatic complexity v(G) is based on the
     85   representation of a method's control flow graph as a directed graph:  
     86 </p>
     87 
     88 <blockquote>
     89   <p>
     90   v(G) = E - N + 2
     91   </p>
     92 </blockquote>
     93 
     94 <p>
     95   Where E is the number of edges and N the number of nodes. JaCoCo calculates
     96   cyclomatic complexity of a method with the following equivalent equation based
     97   on the number of branches (B) and the number of decision points (D):
     98 </p>
     99 
    100 <blockquote>
    101   <p>
    102   v(G) = B - D + 1
    103   </p>
    104 </blockquote>
    105 
    106 <p>
    107   Based on the coverage status of each branch JaCoCo also calculates covered and
    108   missed complexity for each method. Missed complexity again is an indication
    109   for the number of test cases missing to fully cover a module. Note that as
    110   JaCoCo does not consider exception handling as branches try/catch blocks will
    111   also not increase complexity.
    112 </p>
    113 
    114 <h2>Lines</h2>
    115 
    116 <p>
    117   For all class files that have been compiled with debug information, coverage
    118   information for individual lines can be calculated. A source line is
    119   considered executed when at least one instruction that is assigned to this
    120   line has been executed.
    121 </p>
    122 
    123 <p>
    124   Due to the fact that a single line typically compiles to multiple byte code
    125   instructions the source code highlighting shows three different status for
    126   each line containing source code: 
    127 </p>
    128 
    129 <ul>
    130   <li>No coverage: No instruction in the line has been executed (red
    131       background)</li>
    132   <li>Partial coverage: Only a part of the instruction in the line have been
    133       executed (yellow background)</li>
    134   <li>Full coverage: All instructions in the line have been executed (green
    135       background)</li>
    136 </ul>
    137 
    138 <p>
    139   Depending on source formatting a single line of a source code may refer to
    140   multiple methods or multiple classes. Therefore the line count of methods
    141   cannot be simply added to obtain the total number for the containing class.
    142   The same holds true for the lines of multiple classes within a single source
    143   file. JaCoCo calculates line coverage for classes and source file based on the
    144   actual source lines covered.
    145 </p>
    146 
    147 <h2>Methods</h2>
    148 
    149 <p>
    150   Each non-abstract method contains at least one instruction. A method is
    151   considered as executed when at least one instruction has been executed. As
    152   JaCoCo works on byte code level also constructors and static initializers are
    153   counted as methods. Some of these methods may not have a direct correspondence
    154   in Java source code, like implicit and thus generated default constructors or
    155   initializers for constants.
    156 </p>
    157 
    158 <h2>Classes</h2>
    159 
    160 <p>
    161   A class is considered as executed when at least one of its methods has been
    162   executed. Note that JaCoCo considers constructors as well as static
    163   initializers as methods. As Java interface types may contain static
    164   initializers such interfaces are also considered as executable classes.
    165 </p>
    166 
    167 </div> 
    168 <div class="footer">
    169   <span class="right"><a href="@jacoco.home.url@">JaCoCo</a> @qualified.bundle.version@</span>
    170   <a href="license.html">Copyright</a> &copy; @copyright.years@ Mountainminds GmbH &amp; Co. KG and Contributors
    171 </div>
    172 
    173 </body>
    174 </html>
    175