Generate test coverage reports using JaCoCo

0. Why generate a test coverage report?

In our actual work, after the development of the program is completed, it needs to be submitted to testers for testing. Only after testing by testers can the code be put online into the production environment.

One question is: How can we prove that the program has been fully tested and that all the code in the program has been tested. Of course, code coverage can only be used as a reference after testing. If the initial requirements are misunderstood, then no matter how high the code coverage is, it will lose its meaning.

Then you need to introduce code coverage. To measure the code coverage of C++, you can use GCover, and to measure the code coverage of Java, you can use JaCoCo.

JaCoCo is a free Java code coverage detection tool that can count test coverage of the following content:

  • instruction coverage
  • branch coverage
  • Cyclomatic complexity coverage
  • row coverage
  • method override
  • Class coverage

For the specific meaning of various coverage rates, please refer to the official documentation: https://www.jacoco.org/jacoco/trunk/doc/counters.html

When using JaCoCo to generate coverage, you can use the on-the-fly method, which is non-intrusive to the original code of the program. You only need to add a parameter when starting the jar package, which ensures that the code tested by the tester is as consistent as it is online. The code in the production environment is consistent.

1. Create test classes and package them into services

For the sake of simplicity, the class to be tested is created directly based on springboot and uses the RequestMapping annotation.

package com.test.JacocoTest.controler;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class Athytics {

    @GetMapping("/+/{a}/{b}")
    public int add(@PathVariable("a") Integer param1, @PathVariable("b") Integer b){
        int result = 0;

        if(param1 < 0 || param1 > 1000){
            result = b - param1;
        }else{
            result = param1 + b - 1000;
        }
        return result;
    }

    @GetMapping("/-/{a}/{b}")
    public int minus(@PathVariable("a") Integer param1, @PathVariable("b") Integer b){
        return param1 - b;
    }

    @GetMapping("/*/{a}/{b}")
    public int multiply(@PathVariable("a") Integer param1, @PathVariable("b") Integer b){
        return param1 * b;
    }

    @GetMapping("///{a}/{b}")
    public int divide(@PathVariable("a") Integer param1, @PathVariable("b") Integer b){
        return param1 / b;
    }

    @GetMapping("/c/{a}/{b}")
    public String power(@PathVariable("a") Integer param1, @PathVariable("b") Integer b){
        return String.valueOf(Math.round(Math.pow(param1, b)-1));
    }

    @GetMapping("/abs/{a}")
    public String abs(@PathVariable("a") Integer param1){
        return String.valueOf(Math.round(Math.abs(param1)));
    }
}

Create a SpringBoot startup class:

package com.test.JacocoTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JacocoTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(JacocoTestApplication.class, args);

    }
}

The code structure is very simple

2. Start JaCoCo

2.1 Download JaCoCo toolkit

You can go to the official page to download https://search.maven.org/search?q=g:org.jacoco

Just download the two packages you need to use

Jacoco toolkit and tested packages are as follows

2.2 Start the service under test and add JaCoCo to the startup statement

Specify the package name that needs to be checked for test code coverage, and collect the IP and port of the Jacoco coverage file

java -javaagent:jacocoagent.jar=includes=com.test.*,output=tcpserver,port=6301,address=localhost,append=false -jar JacocoTest-1.0-SNAPSHOT.jar

3. Execute test cases

Because the Springboot development framework is used, you can directly open the browser for testing.

Enter the following URL: http://localhost:9999/+/12/25

4. Generate dump and reset coverage statistics

Specify where to obtain jacoco statistics. The address and port should be consistent with the startup parameters in 2.2.

java -jar jacococli.jar dump --address localhost --port 6301 --destfile jacoco.exec --reset

Parameter Description:

--reset After generating coverage data, reset the previous statistics. If you want to re-obtain the statistics, you need to delete the previously generated exec file.

--address IP of the host running jacocoagent.jar

--port host monitoring port running jacocoagent.jar

After execution, the exec file jacoco.exec specified in the instruction will be generated in the directory.

5. Generate coverage report

To generate a report, you need to specify both the source file and the compiled bytecode file.

Specify the class file and source code path, and generate a report style in html format.

java -jar jacococli.jar report jacoco.exec --classfiles  D:\Spring\JacocoTest\target\classes --sourcefiles D:\Spring\JacocoTest\src\main\java --html log

Clicking index.html in the directory will open the coverage information

Enter the test class and you can see the coverage of each method.

Enter the method and you can see the code coverage.

 

Green is fully covered, red is uncovered, and yellow is partially covered.

To obtain new coverage data, you need to delete the exec file generated in step 3, otherwise it will be cumulative coverage.

6 Help

jacococli.jar usage help

Usage: java -jar jacococli.jar report [] [--encoding ] [--help] [--html

] [--name ] [--quiet] [--sourcefiles ] [--tabwith ] [--xml ]

: list of JaCoCo *.exec files to read

--classfiles : location of Java class files

--csv : output file for the CSV report

--encoding : source file encoding (by default platform encoding is

used)

--help : show help

--html

--name : name used for this report

--quiet : suppress all output on stdout

--sourcefiles : location of the source files

--tabwith : tab stop width for the source pages (default 4)

--xml : output file for the XML report

Guess you like

Origin blog.csdn.net/QQ156881887/article/details/129336124
Recommended