一个测试文件与源文件位于不同模块时Jacoco覆盖率配置的例子

问题描述:

  我们有个多模块项目,由于种种原因(更常见的可能是需要集成测试)测试文件和源文件不在一个模块,Jacoco的覆盖率无法正确显示,查询了一些资料,发现中文的例子比较少,就把我自己的Demo贴一下吧。

 该demo 项目有三个子模块,其中sub1,sub2包含各自业务逻辑,demo-test作为测试模块。

 <modules>
        <module>sub1</module>
        <module>sub2</module>
        <module>demo-test</module>
  </modules>

 首先是两个分别位于sub1模块及sub2模块的service,以及demo-test模块下的测试代码

  1.demo/sub1/src/main/java/service/Sub1Service.java

package service;

/**
 * @author tuzhenxian
 * @date 19-7-22
 */
public class Sub1Service {
    public void doSomething(){
        System.out.println("done by sub1");
    }
}

 2.demo/sub2/src/main/java/service/Sub2Service.java

package service;

/**
 * @author tuzhenxian
 * @date 19-7-22
 */
public class Sub2Service {
    public void doSomething(){
        System.out.println("done by sub2");
    }
}

   3.demo/demo-test/src/test/java/IntegrationTest.java

import org.junit.Test;
import service.Sub1Service;
import service.Sub2Service;

/**
 * @author tuzhenxian
 * @date 19-7-22
 */
public class IntegrationTest {
    @Test
    public void testDoSomething(){
        Sub1Service sub1Service=new Sub1Service();
        Sub2Service sub2Service=new Sub2Service();
        sub1Service.doSomething();
        sub2Service.doSomething();
    }
}

  接着是主工程的pom文件,注意声明了goal 为 prepare-agent (这个参数将jacoco的运行时代理作为一个参数传到test,https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <jacoco-version>0.8.3</jacoco-version>
    </properties>

    <groupId>com.zhenxian</groupId>
    <artifactId>demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>sub1</module>
        <module>sub2</module>
        <module>demo-test</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-version}</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

  最后是在demo-test模块下的pom,注意goal为 report-aggregate ,这个goal是jacoco 0.7.7版本以后,专门为多模块覆盖率显示所设置,可以统计该模块所依赖的所有其他模块的覆盖率(如这里的dependency sub1,sub2)。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.zhenxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-test</artifactId>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.zhenxian</groupId>
            <artifactId>sub1</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.zhenxian</groupId>
            <artifactId>sub2</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-version}</version>
                <executions>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

  配置完成,在顶层目录即/demo 下运行 mvn test,即可在 /demo/demo-test/target/site/jacoco-aggregate/index.html 看到覆盖率情况。

一个测试文件与源文件位于不同模块时Jacoco覆盖率配置的例子

参考:https://github.com/jacoco/jacoco/wiki/MavenMultiModule

https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp

 

上一篇:Ubuntu系统中编写c语言程序教程


下一篇:python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时