我正在使用JaCoCo进行代码覆盖.使用junit创建单元测试报告并正确导入它们,以便正确显示单元测试信息.该问题是,我得到的错误消息: 大约每测试覆盖无资料.并且代码覆盖率显示单元测试,集成测试和整体覆盖率的值0%.我检查了sonar-project.properties中的所有必需信息,如binary,src,tests等.
我正在使用:
- SonarQube 4.5.1
- SonarRunner 2.4
- MySQL
- junit 4.1.1
- jacoco 0.7.2
jacoco.exec位于项目基目录中的文件/目标中.
下面你可以看到sonar-project.properties:从我的角度来看,所有必要的路径都设置正确.(即二进制,src,测试)
Comma-separated paths to directories with sources (required)
sonar.sources=src
compiled code
sonar.java.binaries=class
source code of unit tests
sonar.tests=test/src
Comma-separated paths to files with third-party libraries (JAR files in the case of Java)
sonar.java.libraries=jar
Language
sonar.language=java
Encoding of the source files
sonar.sourceEncoding=UTF-8
Additional parameters
sonar.my.property=value
Set Project Base
sonar.projectBaseDir=C:/snapshots/steffen_latest/software/java
Tells SonarQube to reuse existing reports for unit tests execution and …Run Code Online (Sandbox Code Playgroud) 在我的java项目中,我生成的类与其他类位于同一个包文件夹中.我想配置jacoco maven插件来排除那些生成的类,只使用main/src/java文件夹中的类(不是src/main/java-generated)
项目结构:
src/main/java/com/company/john/Good.java <----这包括
src/main/java-generated/com/company/john/AutoGeneratedClass.java <----这个排除
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<includes>
</includes>
<excludes>
<exclude>**/*Dto.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我知道,1个选项是将前缀附加到生成的类,fi _并使用它进行过滤,但我想知道是否还有其他选项.如何指定源项目文件夹(src/main/java),从而排除所有其他文件夹?插件是否仅基于包名称?
我想在我的android项目中生成我的JUnit测试的代码覆盖率报告,所以我添加了JaCoCo gradle插件.这是我的项目级build.gradle文件:
apply plugin: 'jacoco'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-beta6'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
subprojects { prj ->
apply plugin: 'jacoco'
jacoco {
toolVersion '0.7.6.201602180812'
}
task jacocoReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
group = 'Reporting'
description = 'Generate Jacoco coverage reports after running tests.'
reports {
xml {
enabled = true
destination "${prj.buildDir}/reports/jacoco/jacoco.xml"
} …Run Code Online (Sandbox Code Playgroud) 我是Maven的新手,想要使用Jacoco Maven插件来构建我的项目.
我已经建立了一个示例项目,其中TestNG是唯一的依赖项.
这是pom.xml的一部分:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:
生命周期配置未涵盖的插件执行:org.jacoco:jacoco-maven- plugin:0.6.2.201302030002:prepare-agent(执行:默认,阶段:初始化)
我究竟做错了什么 ?干杯
我正在使用JaCoCo,它正在考虑Lombok生成的方法(在字节码中生成,而不是源代码中的跟踪).如何配置JaCoCo忽略它们?
如何在Jacoco Gradle中设置最小代码覆盖率?
如果不满足,我希望构建失败.
我们刚刚将单元测试移植到JUnit5.意识到这仍然是相当早期的采用,在谷歌上几乎没有提示.
最具挑战性的是获得我们在jenkins上使用的Junit5测试的jacoco代码覆盖率.因为这花了我差不多一天才弄明白,我想我分享了.不过,如果您知道更好的解决方案,我很有兴趣知道!
buildscript {
dependencies {
// dependency needed to run junit 5 tests
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
}
}
// include the jacoco plugin
plugins {
id 'jacoco'
}
dependencies {
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2"
runtime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2"
runtime "org.junit.vintage:junit-vintage-engine:4.12.0-M2"
}
apply plugin: 'org.junit.platform.gradle.plugin'
Run Code Online (Sandbox Code Playgroud)
然后问题似乎是org.junit.platform.gradle.plugin中定义的junitPlatformTest在gradle生命周期阶段定义太晚,因此在解析脚本时是未知的.
为了仍然能够定义观察junitPlatformTest任务的jacoco任务,需要以下hack.
tasks.whenTaskAdded { task ->
if (task.name.equals('junitPlatformTest')) {
System.out.println("ADDING TASK " + task.getName() + " to the project!")
// configure jacoco to analyze the junitPlatformTest task
jacoco {
// this tool version is compatible with
toolVersion = "0.7.6.201602180812"
applyTo …Run Code Online (Sandbox Code Playgroud) 我最近搬到了另一台计算机,需要重置我的所有环境。
所以,这个测试之前是有效的。
但我不记得我之前使用的是哪个版本的Java/JDK。
嗯,问题是:
java.lang.instrument.IllegalClassFormatException: Error while instrumenting path/to/class
Run Code Online (Sandbox Code Playgroud)
我正在使用Jacoco“0.8.1”
java --version
openjdk 17.0.1 2021-10-19
Run Code Online (Sandbox Code Playgroud)
和 JDK 1.8
谁能知道如何处理它?
在eclipse中我使用EcLEmma来查看单元测试代码的覆盖范围.工作得很好.因此,我尝试使用maven的jacoco插件从maven构建中看到相同的报告,甚至更好地使用特定的配置文件或站点周期.一切都失败了
所有建议的解决方案对我来说都不起作用
获得unist测试代码覆盖率报告(使用surefire)的最佳方法是什么?[编辑]为了更加灵活,为什么jacoco为我失败了....因为我得到了由于缺少执行数据而跳过JaCoCo执行
来自物业中的pom
<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
Run Code Online (Sandbox Code Playgroud)
在Build部分
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<versionRange>${jacoco.version}</versionRange>
<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>
<!-- Sets the path to the file which contains the execution
data. -->
<destFile>${jacoco.ut.execution.data.file}</destFile>
<!-- Sets the name of the property containing the settings for
JaCoCo runtime agent. …Run Code Online (Sandbox Code Playgroud) 我有一个switch声明从a String和我编写的单元测试中提取一个寻址模式来覆盖,我认为是每个可能性但JaCoCo似乎跳过我的switch陈述,导致覆盖率降低.
为什么,如果我的所有case语句(包括默认值)都在测试中执行,那么该switch语句是否会被计为命中?
(参见CodeCov中显示的测试结果)