gpt4 book ai didi

java - 使用JaCoCo和Gradle进行离线检测

转载 作者:行者123 更新时间:2023-12-03 05:03:58 24 4
gpt4 key购买 nike

我遇到了很多人遇到的同一问题,这在使用Jacoco / Gradle和Powermock时会获得正确的代码覆盖率信息。
我已经在这里和其他地方阅读了所有各种线程,并且已经成功地创建了一个任务(针对Gradle 6.4),该任务对项目的类进行离线检测。供引用的代码如下:

task instrumentClasses(dependsOn: [ classes, project.configurations.jacocoAnt ]) {
inputs.files classes.outputs.files
File outputDir = new File(project.buildDir, 'instrumented')
outputs.dir outputDir
doFirst {
project.delete(outputDir)
ant.taskdef(
resource: 'org/jacoco/ant/antlib.xml',
classpath: project.configurations.jacocoAnt.asPath,
uri: 'jacoco'
)
def instrumented = false
jacocoOfflineSourceSets.each { sourceSetName ->
if (file(sourceSets[sourceSetName as String].output.classesDirs.singleFile.absolutePath).exists()) {
def instrumentedClassedDir = "${outputDir}/${sourceSetName}"
ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
fileset(dir: sourceSets[sourceSetName as String].output.classesDirs.singleFile, includes: '**/*.class')
}
//Replace the classes dir in the test classpath with the instrumented one
sourceSets.test.runtimeClasspath -= sourceSets[sourceSetName as String].output.classesDirs
sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
instrumented = true
}
}
if (instrumented) {
//Disable class verification based on https://github.com/jayway/powermock/issues/375
test.jvmArgs += '-noverify'
}
}
}
现在,在大多数情况下,这似乎可以正常工作。我已经成功验证了我的类(class)已经正确安装了,并且看到Jacoco制作的报告具有正确的信息。问题是尽管我的SonarQube服务器仍然将有问题的类列为未涵盖的类。关于这一点,我不知道我需要做什么来解决它。
作为引用,我正在使用以下版本的sonarqube插件:
"org.sonarqube" version "2.7"
我的CI以以下方式运行Gradle任务:
 - ./gradlew jacocoTestReport sonarqube ${SONAR_GRADLE_EXTRA_PARAMS} -Dsonar.projectKey=${CI_PROJECT_ID} -Dsonar.host.url=${SONAR_URL} -Dsonar.login=${SONAR_LOGIN} -Dsonar.branch.name=${CI_COMMIT_REF_NAME};
我确实知道,SonarQube或我运行Gradle任务的方式一定是一些配置问题,但我不确定是什么原因。

最佳答案

如果您能够生成汇总的jacoco报告(从所有源集中汇总),则只需在运行时的声纳任务中指定该值即可(声纳将仅选择jacoco计算出的确切覆盖率信息)

./gradlew sonarqube -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=XXXX -Dsonar.organization=XXXXX -Dsonar.coverage.jacoco.xmlReportPaths=build/jacoco-report.xml
仅供引用,我在build / jacoco-report.xml中创建汇总报告
以下是我的gradle配置(可能对您有用)
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'jacoco'
id "org.sonarqube" version "2.8"
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11

repositories {
mavenCentral()
}

sourceSets {
intTest {
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath += sourceSets.main.output + sourceSets.test.output
}
}

configurations {
intTestImplementation.extendsFrom testImplementation
intTestRuntimeOnly.extendsFrom testRuntimeOnly
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
useJUnitPlatform()
testLogging.showStandardStreams = true //To print logs
}


task integrationTest(type: Test) {
testClassesDirs = sourceSets.intTest.output.classesDirs
classpath = sourceSets.intTest.runtimeClasspath
shouldRunAfter test
testLogging.showStandardStreams = true //To print logs
}

jacocoTestReport {
executionData(file("${project.buildDir}/jacoco/test.exec"), file("${project.buildDir}/jacoco/integrationTest.exec"))
reports {
xml.enabled true
csv.enabled false
xml.destination file("${buildDir}/jacoco-report.xml")
html.destination file("${buildDir}/jacocoHtml")
}
mustRunAfter(test, integrationTest) // integration tests are required to run before generating the report
}

jacocoTestCoverageVerification {
executionData(file("${project.buildDir}/jacoco/test.exec"), file("${project.buildDir}/jacoco/integrationTest.exec"))
violationRules {
rule {
limit {
counter = 'INSTRUCTION'
minimum = 0.94
}
limit {
counter = 'BRANCH'
minimum = 1.0
}
}
}
}

check.dependsOn(integrationTest, jacocoTestCoverageVerification)

tasks.withType(Test) {
finalizedBy jacocoTestReport
}

关于java - 使用JaCoCo和Gradle进行离线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62577549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com