我遵循标准的Maven模式,在该模式中,我使用单独的模块进行集成测试。这个模块有一个包装类,它执行主要的依赖jar项目。
虽然jar项目有自己的测试用例,但我对执行这些用例不感兴趣。我希望看到由集成测试执行时jar项目中的代码覆盖率。简单,没有报告聚合。
发布于 2017-01-28 03:44:52
让我引用http://www.jacoco.org/jacoco/trunk/doc/report-aggregate-mojo.html,即使它的名称包含一种“聚合”:
这也允许在测试与测试代码不同的项目中创建覆盖报告,例如在集成测试的情况下。
让我们试一试。给定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()方法。
发布于 2017-10-04 22:18:42
通过添加下面这行代码,我已经将一个Java代理附加到应用服务器的standalone.sh (启动脚本):
JAVA_OPTS="$JAVA_OPTS -javaagent:/home/jboss/.m2/repository/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812 -runtime.jar=destfile=/var/lib/jenkins/workspace/HDAP_JaCoCo/jacocoSoapui.exec,includes=*,append=false,output=file"我已经指定了位于我的Jenkins作业的工作区中的目标文件,该作业运行单元测试的JaCoco代码覆盖率(以便获得与我的覆盖率进行比较的类)。
然后,我指定了两个exec文件的路径(一个来自单元测试,另一个是我在上面提到的Jenkins作业的record coverage report部分中为集成测试创建的文件)。然后我们现在有了所有测试的覆盖率。
注意:
需要停止应用程序服务器
https://stackoverflow.com/questions/41885772
复制相似问题