gradle jacocoTestReport不工作?

gtlvzcf8  于 2023-08-06  发布在  其他
关注(0)|答案(5)|浏览(138)

我曾尝试使用gradle jacoco插件在spring-gradle项目中获得代码覆盖率。
gradle包含以下内容

apply plugin: "jacoco"

    jacoco {
        toolVersion = "0.7.1.201405082137"
        reportsDir = file("$buildDir/customJacocoReportDir")
    }

    jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

字符串
然后我就跑了

gradle test jacocoTestReport


之后,在build/reports文件夹中只生成文件test.exec。
除此之外,什么也没发生。

如何获取HTML报告?

lb3vh1jj

lb3vh1jj1#

不需要配置reportsDir/destinationFile

因为jacoco对它们有默认值。
build.gradle:

plugins {
    id 'java'
    id 'jacoco'
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled true
    }
}

repositories {
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

字符串
运行gradle test jacocoTestReport
您可以在./build/reports/jacoco/test目录中找到测试报告。
HTML输出在./build/reports/jacoco/test/html目录中。

dgjrabp2

dgjrabp22#

跟随帮助。它在样品/测试/jacaco的gradle-2.3-all.zip从https://gradle.org/releases/

apply plugin: "java"

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.7.1.201405082137"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile "junit:junit:4.+"
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

字符串

4si2a6ki

4si2a6ki3#

不幸的是,这些答案对我都不起作用。
我也遇到过类似的问题。
唯一不同的是没有生成 exec 文件。
正因为如此,我发现jacocoTestReport只是“跳过”。

我加了修复了:

apply plugin: 'jacoco'

test {
  useJUnitPlatform()
  finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
    ...
    ...
    ...
    ...
}

字符串
那是因为我使用Junit5和spring Boot 2.X

jxct1oxe

jxct1oxe4#

subprojects {
    apply(plugin: 'org.jetbrains.kotlin.jvm')

    repositories {
        jcenter()
        mavenCentral()
   }
}

task codeCoverageReport(type: JacocoReport) {

    // Gather execution data from all subprojects
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects
    subprojects.each {
        sourceSets it.sourceSets.main
    }

    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport.dependsOn {
    subprojects*.test
}

sonarqube {
    properties {
        property "sonar.projectKey", "your_project_key"
        property "sonar.verbose", true
        property "sonar.projectName", "Your project name"
        property "sonar.coverage.jacoco.xmlReportPaths", "${rootDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
    }
}

字符串
运行测试覆盖率的命令:

./gradlew codeCoverageReport
./gradlew sonarqube -x test (test is excluded since already run and sonarqube by default executes test)


如果没有使用sonarqube,则可以忽略第二个命令。
有两件事值得注意,使其工作:
1.为了使所有模块的源集可用,循环子项目并累积源集可以工作。subprojects.sourceSets.main.allSource.srcDirs不工作。
1.sonar.jacoco.reportPaths
弃用
。我们需要使用sonar.coverage.jacoco.xmlReportPath。在此处查看文档

xyhw6mcr

xyhw6mcr5#

使用gradle 8.2构建.gradle文件

sonar {
  properties {
      property 'sonar.host.url', 'http://localhost:9000'
      property 'sonar.login', 
      'squ_b715dcf08655b02c3e32ae1ad0d6987ab239ec64'
      property 'sonar.verbose', true
      property 'sonar.qualitygate.wait', true
      property 'sonar.projectKey', 'sonarqube-jacoco-code-coverage'
  }
}

jacocoTestReport {
  reports {
     xml.required = true
  }
}
test.finalizedBy jacocoTestReport

tasks.named('sonar').configure {
 dependsOn test
}

tasks.named('test') {
  useJUnitPlatform()
}

字符串

相关问题