gpt4 book ai didi

maven - jacoco 简单集成测试解决方案

转载 作者:行者123 更新时间:2023-12-04 23:39:37 24 4
gpt4 key购买 nike

我遵循标准 Maven 模式,我使用单独的模块进行集成测试。这个模块有一个执行主要依赖 jar 项目的包装类。

虽然 jar 项目有自己的测试用例,但我对执行这些用例不感兴趣。我想在集成测试执行时查看 jar 项目中的代码覆盖率。简单,没有报告聚合。

最佳答案

让我引用http://www.jacoco.org/jacoco/trunk/doc/report-aggregate-mojo.html即使它的名称包含一种“聚合”:

This also allows to create coverage reports when tests are in separate projects than the code under test, for example in case of integration tests.



咱们试试吧。给定 jar/src/main/java/example/Example.java :
package example;
public class Example {
// to be covered by unit test
public void a() {
System.out.println("a");
}

// to be covered by integration test
public void b() {
System.out.println("b");
}
}

单元测试 jar/src/test/java/example/ExampleTest.java :
package example;
public class ExampleTest {
@org.junit.Test
public void test() {
new Example().a();
}
}

集成测试 it/src/test/java/example/ExampleITTest.java :
package example;
public class ExampleITTest {
@org.junit.Test
public void test() {
new Example().b();
}
}
pom.xml :
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>jar</module>
<module>it</module>
</modules>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
jar/pom.xml :
<?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>

<parent>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>jar</artifactId>

</project>

最后是最重要的部分 it/pom.xml所有的魔法发生在哪里:
<?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>

<parent>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>it</artifactId>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>jar</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<!--
"report" goal can't cross boundaries of modules,
while "report-aggregate" can, so let's use it, however
by default it will load "jacoco.exec" from this module and from module "jar",
so let's also change file name for this module to avoid intersection
-->
<execution>
<configuration>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
</configuration>
</execution>
<execution>
<id>it-report</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>**/jacoco-it.exec</dataFileIncludes>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

在这样的设置 mvn clean verify将生成两个报告 - jar/target/site/jacoco显示该方法 a()被覆盖, it/target/site/jacoco显示该方法 b()被覆盖。

关于maven - jacoco 简单集成测试解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com