【问题标题】:Jacoco shows coverage 0.0 when running the gradle task but shows correct when run from IntellijJacoco 在运行 gradle 任务时显示覆盖率 0.0,但在从 Intellij 运行时显示正确
【发布时间】:2018-09-10 09:28:10
【问题描述】:

我正在使用 Jacoco 插件来计算 Spring Boot 应用程序的代码覆盖率。 build.gradle 中的 Jacoco 配置如下所示:

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

afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it,
                exclude: ['**/models/**',
                          '**/config/**',
                          '**/constants/**',
                          '**/Application.class'])
    })
}

}

和

jacocoTestCoverageVerification {
violationRules {
    rule {
        element = 'PACKAGE'
        excludes = ['some packages']
        limit {
            counter = 'BRANCH'
            minimum = 0.7
        }
    }
}

}

当我从 IntelliJ 运行覆盖时,它会显示正确的结果,但是当我使用 gradlew clean buils 运行它时,它会失败,覆盖率为 0.0。

我已经编写了一个示例类,这里是 jacoco 报告快照- 举报: 班级: 而我为 SomeClass 写的测试是

public class SomeClassTest {
private SomeClass someClass;

@Before
public void setUp() throws Exception {
    someClass = new SomeClass(12, 23);
}

@Test
public void shouldSumTwoValues() {
    assertThat(someClass.sum()).isEqualTo(35);
}

}

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: jacoco


    【解决方案1】:

    给定src/main/java/SomeClass.java

    class SomeClass {
    
      private Integer value1;
      private Integer value2;
    
      SomeClass(Integer value1, Integer value2) {
        this.value1 = value1;
        this.value2 = value2;
      }
    
      Integer sum() {
        return value1 + value2;
      }
    
    }
    

    src/test/java/SomeClassTest.java

    import org.junit.*;
    import static org.junit.Assert.*;
    
    public class SomeClassTest {
      private SomeClass someClass;
    
      @Before
      public void setUp() throws Exception {
        someClass = new SomeClass(12, 23);
      }
    
      @Test
      public void shouldSumTwoValues() {
        assertEquals(35, (int) someClass.sum());
      }
    }
    

    和build.gradle

    apply plugin: "java"
    apply plugin: "jacoco"
    
    dependencies {
      testCompile "junit:junit:4.12"
    }
    
    repositories {
      mavenCentral()
    }
    

    gradle test jacocoTestReport 的执行在build/reports/jacoco/test/html/index.html 中生成以下报告

    除了build.gradle的

    jacocoTestReport {
      reports {
        xml.enabled false
        csv.enabled false
        html.destination file("${buildDir}/jacocoHtml")
      }
    
      afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['**/models/**',
                              '**/config/**',
                              '**/constants/**',
                              '**/Application.class'])
        })
      }
    }
    
    jacocoTestCoverageVerification {
      violationRules {
        rule {
            element = 'PACKAGE'
            excludes = ['some packages']
            limit {
                counter = 'BRANCH'
                minimum = 0.7
            }
        }
      }
    }
    

    不更改结果/报告,除了更改目录并报告build/jacocoHtml。

    【讨论】:

    • @PoojaGurav 使用包装器 (gradle wrapper --gradle-version 4.10 && ./gradlew clean test jacocoTestReport) 不会改变任何东西。
    【解决方案2】:

    在我的情况下,我发现这是因为我设置了最小值 = 1。当我突然将其更改为 0.9 时,它会起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多