Jacoco在运行gradle任务时显示coverage 0.0,但从Intellij运行时显示正确

时间:2018-09-10 09:28:10

标签: jacoco

我正在使用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运行coverage时,它显示正确的结果,但是当我使用gradlew clean buils运行时,它的覆盖范围为0.0。 enter image description here

我已经编写了一个示例类,这是jacoco报告快照- 报告:enter image description here 类别:enter image description here 我为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);
}

}

有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

给出'test_engine_console.exe' (Win32): Loaded 'C:\vs_projects\test_engine\x64\Debug\test_engine_console.exe'. Symbols loaded. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140d.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Unloaded 'C:\Windows\System32\vcruntime140d.dll' 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'. Cannot find or open the PDB file. 'test_engine_console.exe' (Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'. Cannot find or open the PDB file. The thread 0x4134 has exited with code 0 (0x0). The thread 0x46ac has exited with code 0 (0x0). The thread 0x307c has exited with code 0 (0x0). Detected memory leaks! Dumping objects -> {161} normal block at 0x00000293DD5E2680, 48 bytes long. Data: <G r a p h i c s > 47 00 72 00 61 00 70 00 68 00 69 00 63 00 73 00 {160} normal block at 0x00000293DD5E1820, 16 bytes long. Data: < R] > A0 52 5D DD 93 02 00 00 00 00 00 00 00 00 00 00 Object dump complete. The program '[12764] test_engine_console.exe' has exited with code 0 (0x0).

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中产生以下报告

report

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 } } } } 上的报告的目录之外,不会更改结果/报告。

答案 1 :(得分:0)

就我而言,我发现这是因为我设置了最小值 = 1。当我突然将其更改为 0.9 时,它起作用了