将Cobertura与Gradle和Groovy代码一起使用时报告的测试覆盖率异常低

将Cobertura与Gradle和Groovy代码一起使用时报告的测试覆盖率异常低,groovy,gradle,cobertura,Groovy,Gradle,Cobertura,我正在开发一个Gradle插件,我正在尝试配置我的项目,让我在上面获得代码覆盖率指标。我有基于Spock框架的单元和集成测试 我尝试使用Jacoco和Cobertura来分析我的项目。以下是我正在使用的配置: Gradle: 2.2.1 Groovy: 2.3.6 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_25 (Oracle Co

我正在开发一个Gradle插件,我正在尝试配置我的项目,让我在上面获得代码覆盖率指标。我有基于Spock框架的单元和集成测试

我尝试使用Jacoco和Cobertura来分析我的项目。以下是我正在使用的配置:

Gradle:       2.2.1
Groovy:       2.3.6
Ant:          Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM:          1.8.0_25 (Oracle Corporation 25.25-b02)
OS:           Mac OS X 10.10.1 x86_64
我正在使用gradle cobertura插件v2.2.5

Cobertura

在Cobertura的案例中,我的项目报告的线路覆盖率只有35%。我编写了大量代码测试,但据报告,这些代码没有经过Cobertura的测试:

特别是,Cobertura没有报告嵌套静态类
Version.Parser
的覆盖范围,尽管有一个完整的Spock规范专门用于此:

package com.github.tagc.semver

import spock.lang.Specification
import spock.lang.Unroll

import com.github.tagc.semver.Version.Parser

@Unroll
class VersionParserSpec extends Specification {

    private static final Parser PARSER = Version.Parser.getInstance()

    def "Version information should be extracted from files if parsing is not strict"() {
        given:
        def versionFileText = "version='$versionString'"

        expect:
        PARSER.parse(versionFileText, false) == version

        where:
        versionString      | version
        '0.1.2-SNAPSHOT'   | new Version(0,1,2,false)
        '1.2.4'            | new Version(1,2,4,true)
        '1.3-SNAPSHOT'     | new Version(1,3,0,false)
        '0.4'              | new Version(0,4,0,true)
    }

    def "Valid version representation should be parsed successfully"() {
        expect:
        PARSER.parse(input, true) == version

        where:
        input               | version
        '0.1'               | new Version(0,1,0,true)
        '1.3-SNAPSHOT'      | new Version(1,3,0,false)
        '1.1.1'             | new Version(1,1,1,true)
        '0.2.7'             | new Version(0,2,7,true)
        '0.4.9-SNAPSHOT'    | new Version(0,4,9,false)
        '6.3.16-SNAPSHOT'   | new Version(6,3,16,false)
        '  1.2.3-SNAPSHOT'  | new Version(1,2,3,false)
        ' 1.3.5-SNAPSHOT '  | new Version(1,3,5,false)
    }

    def "Invalid version representation (#input) should cause an exception to be thrown"() {
        when:
        PARSER.parse(input, true)

        then:
        thrown(IllegalArgumentException)

        where:
        input << [
            '1.2.a',
            '1,2,3',
            '2.4.-1',
            '3-4-9',
            '1.4.5-SNPSHOT',
            '1.4.5-SNAPSHOTasd'
        ]
    }
}

Jacoco

相比之下,Jacoco报告了68%的可信覆盖率(根据说明)

相同
版本的覆盖率。解析器
部分报告如下:

我的构建脚本的相关部分包括:

apply plugin: "jacoco"

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath

    jacoco {
        destinationFile = file("$buildDir/jacoco/integrationTest.exec")
        classDumpFile = file("$buildDir/classes/integrationTest")
    }
}

jacocoTestReport {
    executionData test, integrationTest

    reports {
        xml.enabled true
        html.enabled true
    }
}
由于Jacoco似乎工作得很好,我最好还是坚持下去。然而,在Groovy中编写代码时,Sonar似乎不能与Jacoco一起正常工作,所以我似乎被Cobertura卡住了。Cobertura为什么会给我这些报道结果

编辑


我已经在Gradle Cobertura插件Github存储库上进行了测试。

我自己还没有测试过它,但显然这个问题在Gradle插件的v2.2.7版本已经解决,它使用了Cobertura()的v2.1.1版本

我不是StopExcel巨魔,但我认为你应该考虑把修复从上面的链接发布为一个解决方案,除了它没有人看这个想法,这是一个未解决的问题。
apply plugin: "jacoco"

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath

    jacoco {
        destinationFile = file("$buildDir/jacoco/integrationTest.exec")
        classDumpFile = file("$buildDir/classes/integrationTest")
    }
}

jacocoTestReport {
    executionData test, integrationTest

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