我有一个大型的多模块 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 支持 Lambda 函数的旧文章,问题在几年前就得到了解决。
我发现当我运行 Jacoco 时,它没有报告此代码中 Lambda 函数的覆盖率
List<SubmissionStatus> result = jdbcTemplate.query(
FINDALL_SQL,
(rs, rowNum) -> new SubmissionStatus(
rs.getLong("subm_rec_id"),
rs.getLong("subm_file_id"),
rs.getString("contract_id"),
rs.getString("contract_name"),
rs.getString("status"))
);
Run Code Online (Sandbox Code Playgroud)
我知道它受到打击是因为测试无法通过。
我需要为 Jacoco 做一些特别的事情才能正确报告覆盖范围吗?
我正在测试一个项目,并使用 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) 为了生成单元和 Ui 测试的代码覆盖率,我实现了这个jacoco.gradle脚本
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.5"
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
project.afterEvaluate {
android.applicationVariants.all { variant ->
def variantName = variant.name
def testTaskName = "test${variantName.capitalize()}UnitTest"
def uiTestCoverageTaskName = "create${variantName.capitalize()}CoverageReport"
tasks.create(
name: "${testTaskName}Coverage",
type: JacocoReport,
dependsOn: ["$testTaskName", "$uiTestCoverageTaskName"]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build."
reports {
html.enabled = true
xml.enabled = true
}
def excludes = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*Application*.*', …Run Code Online (Sandbox Code Playgroud) 我尝试将我的项目从 JDK 11 升级到 JDK 14,但在将 java 版本设置为 14 后运行测试失败。由于我将 jacoco 与 JMockit 结合使用,我配置了我的构建如下(编辑:JaCoCo 版本是 0.8.3 / 0.8.5,JMockit 版本 1.49):
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${version.jacoco}</version>
<executions>
<execution>
<id>coverage-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>coverage-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire-plugin}</version>
<configuration>
<argLine>
@{argLine} -javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${version.jmockit}/jmockit-${version.jmockit}.jar
</argLine>
</configuration>
</plugin>
...
Run Code Online (Sandbox Code Playgroud)
如果我在 Java 版本设置为 11 的情况下运行 Maven,一切正常,但是当我将 Java 版本设置为 14 时,surefire 插件会抛出此错误:
[ERROR] java.lang.instrument.IllegalClassFormatException: Error while instrumenting sun/util/resources/cldr/provider/CLDRLocaleDataMetaInfo.
[ERROR] at org.jacoco.agent.rt.internal_1f1cc91.CoverageTransformer.transform(CoverageTransformer.java:93)
[ERROR] sun.util.locale.provider.LocaleDataMetaInfo: Unable to load sun.util.resources.cldr.provider.CLDRLocaleDataMetaInfo …Run Code Online (Sandbox Code Playgroud) 我lombok.config在根目录中创建了一个包含以下内容的文件:
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Run Code Online (Sandbox Code Playgroud)
但 Lombok 生成的代码(Getters、Setters、Builders 等)仍然出现在我的 Jacoco 测试报告上。
Jacoco 版本是 0.8.6,Lombok 版本是 1.18.12。
如何从报告中删除 Lombok 代码?
我们最近将 lombok 添加到我们的项目中,但是它降低了 Intellij 中的测试覆盖率,因为它检测到 getter/setter 未经测试。
我们已经添加了 lombok.config 来添加 @Generate 注释,但是只有 JaCoCo 会忽略它们。这适用于 SonarQube,但在 Intellij 中使用 JaCoCo 作为覆盖运行器不起作用,因为它不适用于 powermock。到目前为止,我们发现的唯一解决方法是修改 pom.xml 以使用 JaCoCo 离线工具,运行 Maven 测试,然后手动导入测试覆盖率,但这不是一个非常干净的解决方案。
有没有办法让 Intellij 覆盖率运行程序忽略 @Generate 带注释的方法?如果失败,我们如何设置 JaCoCo 以便我们可以从 intellij 运行测试,而不必执行 Maven 测试解决方法?
以下是我的声纳属性片段:
sonarqube {
properties{
property "sonar.junit.reportPaths", "build/test-results/testDebugUnitTest/*.xml"
property("sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacocoTestReport.xml"
}
}
Run Code Online (Sandbox Code Playgroud)
Jacoco 配置和属性工作正常,我如何确认这一点?我创建了一个 java 类并为其编写了一个单元测试,sonarqube 识别了这一点并将其记录为代码覆盖率的一部分,而它基本上忽略了所有 Kotlin 文件测试。我继续将 kotlin 文件更改为 java,并为其编写了一个 UnitTest,是的,它被识别并添加为代码覆盖率的一部分,再次,kotlin 文件测试被忽略。
顺便说一句,下面是我的 Jacoco.gradle:
apply plugin: 'jacoco'
ext {
coverageExclusions = [
'**/*Activity*.*',
'**/*Fragment*.*',
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
]
}
jacoco {
toolVersion = '0.8.6'
reportsDir = file("$buildDir/reports")
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
tasks.withType(Test) {
finalizedBy jacocoTestReport // report is always generated after tests run
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group = "Reporting" …Run Code Online (Sandbox Code Playgroud) 我的 Android 应用程序中有多个模块。我需要在 CI/CD 管道中自动执行代码覆盖率报告,其中 \xe2\x80\x99t 没有物理/虚拟 Android 设备,并且无法附加设备。
\n集成 Jacoco 来自 - https://github.com/gouline/android-samples/blob/master/jacoco/jacoco.gradle
\n当我尝试通过 gradle 命令生成覆盖率报告时
\n>> ./gradlew createDebugCoverageReport\nRun Code Online (Sandbox Code Playgroud)\n它失败并显示以下错误日志
\n* What went wrong:\nExecution failed for task \':app:connectedDebugAndroidTest\'.\n> com.android.builder.testing.api.DeviceException: No connected devices!\nRun Code Online (Sandbox Code Playgroud)\n所以我尝试通过命令排除 gradle 任务 (connectdDebugAndroidTest)
\n>> ./gradlew createDebugCoverageReport -x app:connectedDebugAndroidTest -x module1:connectedDebugAndroidTest\nRun Code Online (Sandbox Code Playgroud)\n出现如下错误 -
\n> Task :module1:createDebugAndroidTestCoverageReport FAILED\n\nFAILURE: Build failed with an exception.\n\n* What went wrong:\nExecution failed for task \':vpn:createDebugAndroidTestCoverageReport\'.\n> java.io.IOException: No coverage data to process in directories [/Users/abc/ws/prjName/module1/build/outputs/code_coverage/debugAndroidTest/connected]\n …Run Code Online (Sandbox Code Playgroud)