raf*_*raf 28 code-coverage maven maven-surefire-plugin maven-failsafe-plugin jacoco
我正在使用maven与JaCoCo插件生成failafe和surefire报告,但我只能设法将它们放在单独的报告中.我想有一个整体覆盖视图(单元测试和集成测试之间的合并).
在我认为彻底的谷歌搜索之后,我只能找到一种方法来与Sonar一起做.有没有更简单的方法来做到这一点?
相关问题:Maven分离单元测试和集成测试
Cha*_*Hey 29
我最近实现了这个:经过一些令人头疼的事情和大量的测试后,我的配置运行得非常好.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>testArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<execution>
<id>merge-results</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}/coverage-reports</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${project.build.directory}/coverage-reports/aggregate.exec</destFile>
</configuration>
</execution>
<execution>
<id>post-merge-report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/aggregate.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<skipTests>${skip.unit.tests}</skipTests>
<includes>
<include>**/*UT.java</include>
<include>**/*MT.java</include>
<include>**/*Test.java</include>
</includes>
<skipTests>${skipUTMTs}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>${skipTests}</skipTests>
<skipITs>${skipITs}</skipITs>
<argLine>${testArgLine}</argLine>
<excludes>
<exclude>**/*UT*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
如您所见,有6个不同的Jacoco执行来运行测试,合并文件并创建聚合报告.在Jacoco配置之上,您还需要配置Surefire和Failsafe从Jacoco获取参数(Surefire运行单元测试,Failsafe运行集成测试).
我使用的所有配置都应该在那里,你用它做的是你的设计架构,使它符合你想要的要求.就个人而言,如果您遇到文件未被阅读的问题,我建议您查看我排除的内容并包含在surefire和failsafe中.
Gui*_*ume 23
不是你想要的答案,但仍然......
在大多数情况下,您不应合并单元和集成测试的覆盖范围.
单元测试的价值在于它们改进了应用程序的设计,并确保代码的极端情况正常工作.您应该尝试对单元测试进行高分支覆盖.
集成测试的价值在于它们确保应用程序的主要用例正常工作,并确保整个堆栈正确集成.您应该尝试为集成测试提供高功能覆盖率.(但用工具测量功能覆盖率相当困难).
如果您需要集成测试来改善分支机构的覆盖范围,那么您就应该检查代码的设计.如果您在没有集成测试的情况下已经拥有较高的分支覆盖率,那么添加它们不应该显着修改您的指标
随意投票给这个答案,因为这是一个主题和相当自以为是的...