我正在开发一个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-plugin 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'
]
}
}
以下是我的Gradle构建脚本的相关部分:
buildscript {
repositories { jcenter() }
dependencies {
// Cobertura plugin
classpath "net.saliman:gradle-cobertura-plugin:2.2.5"
}
}
configurations.all {
resolutionStrategy {
force 'org.ow2.asm:asm:5.0.3'
forcedModules = [ 'org.ow2.asm:asm:5.0.3' ]
}
}
apply plugin: 'net.saliman.cobertura'
check.dependsOn 'cobertura'
cobertura {
coverageFormats = [ 'html', 'xml' ]
}
Jacoco
相比之下,Jacoco报告的更为合理的覆盖率为68%(按指示)。
相同Version.Parser
部分的覆盖范围报告为:
我的构建脚本的相关部分是:
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存储库上有raised this as an issue。