我知道我可以深入查看仪表板上的覆盖率小部件并查看代码和分支覆盖率。但是,我想查看每个单独的测试涵盖哪些源文件和代码行。看起来声纳不支持这种相反的方式。如果不支持,是否可以配置 Sonar 来执行此操作?仅供参考,我们现在正在使用 JaCoCo 进行测试覆盖率。任何信息都非常感谢!
在父 POM 中,我们设置了 Jacoco 规则来强制执行测试覆盖率。这包括通常没有行为的类的一些排除:
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<excludes>
<!-- exclude largely auto-generated domain and model classes -->
<exclude>**/model/*.class</exclude>
<exclude>**/model/**/*.class</exclude>
<exclude>**/domain/*.class</exclude>
<exclude>**/domain/**/*.class</exclude>
<exclude>**/dto/*.class</exclude>
<exclude>**/dto/**/*.class</exclude>
</excludes>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
Run Code Online (Sandbox Code Playgroud)
在使用此父项的子 POM 中,添加额外的排除类模式的 Maven 魔力是什么?
我尝试combine...以各种方式使用属性,但无法正确输出有效的 POM。
有任何想法吗?
构建工具:Maven
使用离线检测的原因:删除 Powermock 不是一个选项
问题:failsafe 和 Surefire 均运行并生成报告。但是,jacoco.exec 是生成的,但 jacoco-it.exec 不是。除了 IT 之外,离线检测、覆盖范围和报告也运行良好。
这是我使用的 Maven 插件配置:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
为了运行测试,我使用 Maven clean install。 …
我们有一个 Android 项目,我们在一些测试用例中使用 Powermock,在覆盖率报告中使用 Jacoco。我们注意到我们的一些类返回0% 的覆盖率,尽管它们确实被覆盖了。我们还针对受影响的课程观察到以下消息。
"Classes ... do no match with execution data."
Run Code Online (Sandbox Code Playgroud)
一些在线搜索表明,Powermock 和 Jacoco 不能很好地发挥作用,而离线仪器是一种可能的解决方法。
有没有人以前在android项目中使用过gradle离线检测脚本?
我使用以下教程https://proandroiddev.com/unified-code-coverage-for-android-revisited-44789c9b722f在我的 Android 项目中实现了 Jacoco,以满足 kotlin 类中的测试覆盖率。
由于某些未知原因,它没有报告在 Companion 块下声明的静态方法的覆盖率。
class Meh {
companion object {
fun test () {
// logic to test
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将类转换为实例而不是单例,则我可以完全看到覆盖范围。
有没有人遇到过这个问题?你做了什么?
我有 jenkins 文件,它具有 publishHTML 方法来生成带有 jacoco 结果的 .html 文件的报告。是否可以在仪表板上显示该报告,例如“测试趋势结果”?
我想展示使用 Gradle 6.0 构建的多个项目 Spring boot 应用程序的测试覆盖率。我们目前使用 JUnit5。
尽管存在一些初步测试,SonarQube 中的测试覆盖率显示为 0%。
顶级项目(https://github.com/OpenReqEU/eclipse-plugin-vogella/blob/master/server/build.gradle)中的 build.gradle 文件具有以下输入:
plugins {
id "org.sonarqube" version "2.7"
id 'jacoco'
}
repositories {
jcenter()
}
subprojects {
apply plugin: 'jacoco'
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
jcenter()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
jacocoTestReport {
reports {
xml.enabled true
xml.destination file("${buildDir}/coverage-reports/coverage.xml")
//html.destination file("${buildDir}/coverage-reports")
}
}
ext {
springBootVersion = …Run Code Online (Sandbox Code Playgroud) 在我的 gradle 版本中,我有 2 个这样的测试任务:
task testAAA(type: Test) {
filter {
includeTestsMatching "*AAA*"
}
finalizedBy jacocoTestReport
}
Run Code Online (Sandbox Code Playgroud)
和
task testBBB(type: Test) {
filter {
includeTestsMatching "*BBB*"
}
finalizedBy jacocoTestReport
}
Run Code Online (Sandbox Code Playgroud)
这会在 build/jacoco 中生成 2 个 .exec 文件:
测试AAA.exec
testBBB.exec
我想生成一个单一的覆盖率报告,它从两个/所有 .exec 文件中获取输入,我试过这个:
jacocoTestReport {
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
reports {
xml.enabled true
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试时,我收到此错误:
Execution failed for task ':Project1:jacocoTestReport'.
> Unable to read execution data file Project1/build/jacoco/test.exec
Project1/build/jacoco/test.exec (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
当我明确提供 executionData 规范时,为什么 jacocoTestReport 寻找“test.exec”?
我正在按照这篇文章为我的项目设置 Jacoco 代码覆盖率。
但我在编写生成报告的任务时遇到错误。
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code_coverage/debugAndroidTest/connected/**/*.ec'
])
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误是:
无法为 org.gradle.testing.jacoco.tasks.JacocoReport 类型的任务 ':app:jacocoTestReport' 设置只读属性 'sourceDirectories' 的值。
jacoco我相信下面的字段现在是私有的。
我的依赖:classpath "org.jacoco:org.jacoco.core:0.8.4"
另外,我尝试更改版本但没有帮助。
最近我在intellj idea中使用JMockit和Junit4进行单元测试。当我使用“运行”按钮或“调试”按钮运行测试时,它工作正常。当我尝试使用“运行覆盖率”按钮获取代码覆盖率结果时,它会出现 ClassFormatError。
sun.instrument.InstrumentationImpl.redefineClasses0(本机方法)处的 java.lang.ClassFormatError
仅当我尝试模拟我想要测试的类中的方法时才会发生这种情况,例如
new Expectations(BaseValidator.class) {
{
BaseValidator.isExistAirLineByTwoCode(anyString);
returns(false, true);
}
};
Run Code Online (Sandbox Code Playgroud)
我在“BaseValidator”类中测试了另一个方法,该方法调用方法“isExistAirLineByTwoCode”。
不知道这个问题的原因是idea还是jmokit,甚至是junit。顺便说一句,我使用 jacoco 来生成代码覆盖率报告。
我该如何解决这个问题?