introduce

JaCoCo (Java Code Coverage) is a tool to analyze unit test Coverage. After running unit tests, you can show which parts of the Code are detected by unit tests, which parts are not, and the percentage of unit test Coverage of the whole project, which can be seen at a glance.

JaCoCo generates metrics for the following metrics:

Instructions (C0 Coverage)

  • It mainly calculates the coverage of bytecode files.

Branches (C1 Coverage)

  • JaCoCo also computes branching over all if and switch statements. It’s mainly about computing branches.
  • No coverage: No branch in the line has been executed (red diamond)
  • Partial coverage: Only part of the branch in the line has been executed (Yellow Diamond)
  • Full coverage: executed at all branches of the bank (green diamond)

Cyclomatic Complexity

  • Cyclomatic Complexity is a measure of code Complexity. It can be used to measure the complexity of a module judging structure, which can be expressed as the number of independent current paths or the number of least used test cases covering all possible situations. High cyclomatic complexity indicates that the program code has complex judgment logic and may be of low quality and difficult to test and maintain. High cyclomatic complexity has a lot to do with the possibility of program errors. Note that JaCoCo does not consider the try-catch block branch of exception handling nor does it add complexity. There’s a positive correlation between the population and the branch. In fact, various studies over the past few years have determined that methods with cyclomatic complexity (or CC) greater than 10 for a method have a high risk of error.
  • For an understanding of cyclomatic complexity, see the following links.

Lines

  • It mainly calculates the actual source line classes and source file line overlays based on overlays. Three states are typically identified.
  • No override: Any instruction executed in the line (red background)
  • Partial coverage: Only part of the instruction in the line has been executed (yellow background)
  • Full coverage: All instructions in this line have been executed (green background)

Methods

  • Each nonabstract method contains at least one instruction. Constructors and static initializers count as methods.

Unit testing

The Eclipse plug-in EclEmma

The installation of EclEmma

1. Start Eclipse and click the Help menu to Install New Software. In the dialog box that pops up, click Add

2. Enter Name, for example, EclEmma. Type Location:update.eclemma.org/

3. In the Work With area, select the Location you just entered

4. Restart Eclipse after the installation. If the installation is successful, a new button will appear on the toolbar

The use of EclEmma

1. Write unit test cases. The following tests are now supported:

  • Local Java application

  • Eclipse/RCP application

  • Equinox OSGi framework

  • JUnit test

  • TestNG test

  • JUnit plug-in test

  • JUnit RAP test

  • SWTBot test

  • Scala application

        2. Using JUnit As an example, right-click on the Test case file and choose Coverage As -> JUnit Test

        3. The running result will be displayed in the test file:

        • Green: Complete execution
        • Red: Not executed
        • Yellow: Partial execution

        4. Click to bring up the Coverage window, which is used to count the Coverage test rate of the program

        5. Click the button in the red rectangle box to view the coverage data of multiple tests

        6. Right-click in the Coverage view main area and choose “Export Report…” from the shortcut menu that appears.

        7. The Export screen is displayed, and the options are as follows:

        • Available Sessions: session to be exported
        • Format: Select the report type (HTML/XML/Text/EMMA Session)
        • Destination: store the exported session

        The IDEA of the plugin

        IDEA comes with Jacoco unit test analysis tool. The procedure is as follows. 1. Select Edit Configurations.



        2. Add a unit test type and select the tested file. In this example, test all code (excluding lib).



        3. Select Jacoco.



        4. Run test cases in coverage mode.



        5. Automatically generate a test report after running.



        MAVEN

        After adding the following plug-in to the pom.xml file, run MVN test to generate the report in the target/site/ Jacoco folder.

        If you want to skip the failed test cases, please use the MVN test – Dmaven. Test. Failure. Ignore = true

        <plugin>    <groupId>org.jacoco</groupId>    <artifactId>jacoco-maven-plugin</artifactId>    <version>0.8.2 - the SNAPSHOT</version>    <executions>        <execution>            <id>default-prepare-agent</id>            <goals>                <goal>prepare-agent</goal>            </goals>        </execution>        <execution>            <id>default-report</id>            <phase>test</phase>            <goals>                <goal>report</goal>            </goals>        </execution>        <execution>            <id>default-check</id>            <goals>                <goal>check</goal>            </goals>        </execution>    </executions></plugin>Copy the code

        Run time testing

        Jacoco supports monitoring the execution of an application while it is running. The following describes two monitoring modes: direct running and Tomcat server running.

        To prepare

        Download the jacoco package at www.jacoco.org/jacoco/

        The downloaded package includes three jars, which are used here: jacocoAgent.jar and jacococli.jar

        Run directly

        1. If you run test.jar directly, run the following command:

        java -javaagent:jacoco\jacocoagent.jar=includes=* -jar test.jar

        The jar package to be prepared is marked by the underscore, followed by the parameter.

        Relevant parameters refer to: www.jacoco.org/jacoco/trun…

        2. After the program is run, the jacoco. Exec file will be generated in the same directory as test.jar.

        3. Obtain the class files to analyze. Assume that the files generated from the source code in test.jar are in the com folder, and place this folder in the same folder as Jacoco-exec.

        4. Run the following command to generate a report:

        java -jar jacoco\jacococli.jar report jacoco.exec –classfiles com –html report

        The underline indicates the jar package in preparation, report indicates that the report is generated, jacoco.exec indicates that the monitoring file is run, – classfiles indicates the classfile that generates the report, – HTML indicates the report format, and report indicates the report folder. (The class file here should be the same as in operation)

        Detailed parameter please refer to: www.jacoco.org/jacoco/trun…

        Tomcat run

        Tomcat runs in much the same way as until it runs, with the only difference being the way jacocoAgent.jar is specified.

        In Windows, add the following Settings before $TOMCAT_HOME/bin/catalina.bat and in Linux:

        set “JAVA_OPTS=-javaagent:=jacoco\jacocoagent.jar=includes=*”

        If the exec file generation path is not specified in the argument, it is generated in the $TOMCAT_HOME/bin folder, and the rest is the same as above.