簡體   English   中英

隨着遷移到Java 11,Jacoco代碼覆蓋率下降

[英]Jacoco code coverage dropped with migration to Java 11

我有幾個使用Java 8構建的Gradle項目,並且在最近將它們轉換為使用Java 11之后,Jacoco代碼覆蓋率報告的報告百分比比之前低得多。 在一個項目中,轉換后立即將我的覆蓋率從81%降至16%。

我嘗試將Jacoco插件更新為0.8.3(具有JDK 11官方支持 ),Gradle更新為5.4,TestNG更新為6.14.3(不確定這是否有任何影響;認為最新版本不會受到影響)。 即使在這些變化之后,我上面提到的項目也有16%的覆蓋率。 我手動檢查了一些報告0%覆蓋率的類,發現它們確實對它們進行了測試覆蓋。

例如,我將此方法添加到我的一個類中:

public String helloWorld(){
        return "hello";
    }

然后我在測試中使用它:

@Test(groups = IntegrationTest.INTEGRATION_GROUP)
    public void testHelloWorld() {
        String helloWorld = authManager.helloWorld();
        assertEquals(helloWorld, "hello");
    }

覆蓋范圍報告為0:

在此輸入圖像描述

如果它有用,這是我的Jacoco Gradle設置。 我正在使用自定義插件來配置它們。

  class ManagedJacocoPlugin implements ManagedPlugin {
  @Override
  void apply(PluginManager pluginManager) {
    pluginManager.apply(JacocoPlugin.class)
  }

  @Override
  void configure(Project project, GradlePluginConfig pluginConfig) {
    def jacoco = project.extensions.getByName("jacoco")
    jacoco.toolVersion = "0.8.3"

    def jacocoTestReport = project.tasks.getByName('jacocoTestReport')
    jacocoTestReport.reports {
      xml.enabled false
      csv.enabled false
    }

    project.tasks.withType(Test).each { t ->
      t.jacoco {
        destinationFile = project.file("$project.buildDir/jacoco/test.exec")
      }
    }

    jacocoTestReport.dependsOn "integrationTest"
  }
}

就我能夠收集的內容而言,鑒於我正在使用的工具版本,應該完全支持Java 11的Jacoco覆蓋。 我在這里錯過了什么?

以下是https://stackoverflow.com/help/mcve關於如何創建最小,完整和可驗證示例的頁面:

確保它完整

將問題中的代碼復制到新文件或項目中,然后運行它。 如果它沒有為你運行,那么它將無法為其他任何人運行。

但是誰知道你的例子中ManagedPlugin是什么?

但好吧,讓我們嘗試按照上述建議並使用我們所擁有的,假裝我們有時間猜測,我們很幸運能夠正確猜測。

添加了許多缺失部分之后,除ManagedPlugin之外的所有內容都將成為以下build.gradle

apply plugin: 'java'
apply plugin: 'jacoco'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
}

test {
    useTestNG() {
        includeGroups('unit')
    }
}

task integrationTest(type: Test, dependsOn: ['test']) {
    useTestNG() {
        includeGroups('integration')
    }
}


def jacoco = project.extensions.getByName("jacoco")
jacoco.toolVersion = "0.8.3"

def jacocoTestReport = project.tasks.getByName('jacocoTestReport')
jacocoTestReport.reports {
    xml.enabled false
    csv.enabled false
}

project.tasks.withType(Test).each { t ->
    t.jacoco {
        destinationFile = project.file("$project.buildDir/jacoco/test.exec")
    }
}

jacocoTestReport.dependsOn "integrationTest"

方法helloWorld進入src/main/Example.java

class Example {
    public String helloWorld() {
        return "hello";
    }
}

方法testHelloWorld進入src/test/ExampleTest.java

import org.testng.annotations.Test;
import static org.testng.Assert.*;

class ExampleTest {
    Example authManager = new Example();

    @Test(groups = "integration")
    public void testHelloWorld() {
        String helloWorld = authManager.helloWorld();
        assertEquals(helloWorld, "hello");
    }
}

使用Gralde 5.4和JDK 11.0.1執行gradle clean jacocoTestReport會生成以下報告

報告1

因此,我們可以得出結論,提供的示例肯定是不完整的。

讓我們嘗試猜測againg並添加到src/main/java/Example.java

    public void anotherMethod() {
    }

並進入src/test/java/ExampleTest.java

    @Test(groups = "unit")
    public void test() {
       new Example().anotherMethod();
    }

現在執行gradle clean jacocoTestReport會生成以下報告

報告2

似乎現在我們可以重現您的問題。

為什么不包括anotherMethod 讓我們按照https://stackoverflow.com/help/mcve的另一個好建議:

分而治之。 如果您有少量代碼,但問題的根源完全不清楚,請一次開始刪除代碼直到問題消失 - 然后再添加最后一部分。

這不僅適用於代碼,也適用於版本的更改 - 讓我們嘗試將Gradle版本的更改從5.4更改為4.10.3並執行gradle clean jacocoTestReport生成

報告3

因此,我們可以得出結論, Gradle中的某些內容已被更改 讓我們檢查一下它的更改日志 - https://docs.gradle.org/5.0/release-notes.html包含一個非常有趣的聲明:

JaCoCo插件現在可以使用構建緩存和並行測試執行

...使用代碼覆蓋率運行的任務被配置為在開始執行之前刪除執行數據...

Task integrationTest刪除任務test收集的數據。 我們試着不要使用同一個文件:

//project.tasks.withType(Test).each { t ->
//    t.jacoco {
//        destinationFile = project.file("$project.buildDir/jacoco/test.exec")
//    }
//}

jacocoTestReport.executionData(test)
jacocoTestReport.executionData(integrationTest)

現在即使使用Gradle 5.4也可以執行gradle clean jacocoTestReport

報告4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM