我的假设场景是这样的:
现在我的 git diff 和 jacoco 信息的交集是覆盖了更改的 10 行代码中的 7 行。即 - 我对新代码有 70% 的覆盖率。
但我必须手动解决这个问题。
我想要一种自动化的方法来计算覆盖了多少新代码行的百分比。
我的问题是:有没有办法只在新代码上获得单位覆盖率(例如来自 jacoco)?
(请注意,如果您运行扫描仪并使用 JSON API 询问任务结果,我知道sonarqube可以做到这一点analysis.mode=publish- 我正在寻找开发人员可以在本地运行的轻量级东西。)
我有一个大型的多模块 Maven 构建,目前混合了 PowerMock 和 Mockito 测试(很快将所有 PowerMock 测试移至 Mockito)。父 pom 中的默认 jacoco-maven-plugin 配置用于“离线”检测,但带有 Mockito 测试的模块正在使用在线检测。我相信模块中的每个 jacoco.exec 文件都已正确构建。
其中一个子模块称为“jacoco-aggregate”,只是尝试使用“合并”和“报告聚合”目标。我使用“合并”是因为我正在与 SonarQube 集成,并且我们使用的版本仅允许单个执行文件。从输出中我可以看到,“合并”目标似乎运行正常。
“报告汇总”目标似乎有问题。目前它根本没有产生任何报道。显示的表是空的。
以下是我在 jacoco 子模块本身中构建时得到的当前输出:
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jacoco-aggregate ---
[INFO] Deleting <myhome>\git\oce_usl\usl-parent\jacoco-aggregate\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (filter) @ jacoco-aggregate ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory <myhome>\git\oce_usl\usl-parent\jacoco-aggregate\src\main\resources
[INFO]
[INFO] --- depends-maven-plugin:1.2:generate-depends-file (generate-depends-file) @ jacoco-aggregate ---
[INFO] Created: <myhome>\git\oce_usl\usl-parent\jacoco-aggregate\target\classes\META-INF\maven\dependencies.properties
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report (default-report) @ jacoco-aggregate ---
[INFO] Skipping JaCoCo …Run Code Online (Sandbox Code Playgroud) 我们正在使用Jacoco和eclemma来进行测试用例覆盖。对于不使用 PowerMockRunner 的类,我们在两者中都获得了正确的覆盖率。对于使用 PowerMockRunner 的类,我们面临着覆盖率问题,例如它在 jacoco 中显示 0% 覆盖率,但在 eclemma 中显示正确的覆盖率。
PowerMockito version :1.7.1
Jdk 1.8
Jacoco:0.7.9
Run Code Online (Sandbox Code Playgroud)
我也尝试使用 PowerMockRunner 规则,但这会导致另一个与验证相关的错误stackframe,该错误很难修复。Eclemma用作eclipse插件,jacoco用作maven插件。任何遇到过这个问题并且能够解决这个问题的人都可以对此有所了解。我已经浏览了很多链接,但没有一个对我有用。
一些有用的参考:
我有一个 Maven 多模块项目,其中 JaCoCo 没有根据源文件生成报告。
通常,如果MyService是正在测试的类,它将以两种方式报告,一种通过MyService.html基于适当包的位置中的文件名和方法列表,以数字和图表的形式提供覆盖范围的整体图 - 列出所有方法在类中,每个方法都有一个指向另一个 html 的可点击链接,MyService.java.html其中包含带有红色/绿色/黄色背景的源代码以显示覆盖状态。
在我的场景中,只MyService.html生成而不是MyService.java.html和 方法在前者中列出并包含覆盖详细信息,但没有指向其他报告的超链接,如下所示。
问题在这里变得更有趣,我的 Maven 配置未在此模块中配置,但在父模块和其他子模块中能够正确生成报告。下面是maven插件配置供参考。
父 POM:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${project.basedir}/target/jacoco-unit.exec</destFile>
<dataFile>${project.basedir}/target/jacoco-unit.exec</dataFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
子POM:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
Run Code Online (Sandbox Code Playgroud)
尝试切换 JaCoCo 版本,但没有任何帮助,现在坚持使用最新的 0.8.2。Maven 是最新的 - 3.6.0。除此之外,子 pom 中配置的另一个插件是 PMD——它的存在与否对报告没有任何影响。
我正在测试一个项目,并使用 Codecov 发布测试的覆盖率。Codecov 使用 Jacoco 生成的报告,到目前为止效果良好。Codecov 不仅显示覆盖率,还显示测试的复杂度。
我对这个复杂率有两个问题,我在文档中找不到答案:
复杂度到底是多少?Codecov 如何衡量它?
所测试的项目是一个maven多模块项目。当我在 POM 中激活 jacoco 插件的 report-aggregate-goal 时,为了聚合每个模块的报告,结果在 codecov 上不会显示复杂性:
为什么会这样呢?
我在 pom 中的 jacoco 插件配置如下
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${jacoco.ut.execution.data.file}</destFile>
</configuration>
</execution>
<execution>
<id>merge-execs</id>
<phase>pre-site</phase>
<inherited>false</inherited>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>**/target/*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${jacoco.ut.merged.exec}</destFile>
</configuration>
</execution>
<execution>
<id>jacoco-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
<dataFile>${jacoco.ut.merged.exec}</dataFile>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
但是当我运行时mvn jacoco:check它失败并出现以下错误
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:check (default-cli) on project main: The parameters 'rules' for …Run Code Online (Sandbox Code Playgroud) 我无法让 sonarQube 输出我的 java Spring 项目的覆盖率报告。因此,它始终在界面中显示 0.0% 的覆盖率。我按照这个操作,它仍然不会生成文件。以下是pom.xml的相关部分,底部是日志。默认情况下,覆盖率报告应该在target/site/jacoco/jacoco.xml;,但是即使我注释掉 <sonar.converage.jacoco.xmlReportPath/> 它仍然不输出任何内容。
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/site/jacoco-aggregate/jacoco.xml
</sonar.coverage.jacoco.xmlReportPaths>
<sonar.exclusions>
**/atlas/base/api/config/*.java,
**/atlas/base/api/model/*.java,
**/atlas/base/api/repository/*.java
**/atlas/bse/api/ClientIntegrationApplication.java
</sonar.exclusions>
<!-- JaCoCo Properties -->
<jacoco.version>0.8.7</jacoco.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<!-- <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>-->
<sonar.language>java</sonar.language>
.
.
.
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
</dependency>
.
.
.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
[INFO] --- sonar-maven-plugin:3.9.0.2155:sonar (default-cli) @ **** ---
[INFO] User cache: C:\Users\***\.sonar\cache
[INFO] SonarQube version: 9.1.0
[INFO] Default …Run Code Online (Sandbox Code Playgroud) 我使用 Jacoco 作为在我的 pom.xml 中配置的代码覆盖插件。我只想测试和分析类文件中少数方法的覆盖率,并且只想相应地显示它们的覆盖率百分比。但当 jacoco 分析整个文件时,它显示的覆盖率较低,尽管相关方法已 100% 覆盖。jacoco 有什么方法可以在不更改源文件代码的情况下排除某些正在分析的方法吗?
我正在尝试使用 JaCoCo 在这个项目中测量覆盖率:https : //github.com/square/retrofit
一切似乎都运行良好,但由于某种原因,一些曾经可以工作的测试在使用 JaCoCo 运行时代理时失败了。
这是我的(有趣的部分)pom.xml:
...
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration> …Run Code Online (Sandbox Code Playgroud) Jacoco 插件丢失了 JUnit 测试,报告始终为 0%,但测试存在。我认为问题是我的pom.xml。帮我找出错误。
也许它与插件冲突checkstyle?
部分 Jacoco 设置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<reportFormat>plain</reportFormat>
<includes>
<include>**/*Test*.java</include>
<include>**/*IT*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration> …Run Code Online (Sandbox Code Playgroud) jacoco ×8
java ×5
maven ×5
junit ×2
codecov ×1
eclemma ×1
powermockito ×1
sonarqube ×1
unit-testing ×1