gradle build正在创建一个jacoco报告,该报告由声纳跑步者接收。将声纳结果推送到声纳服务器后,即使所有测试都成功运行,也会显示单元测试覆盖率,但单元测试成功为0。
查看声纳跑步者日志,我看到以下项目报告了源和类位置:
15:52:36.087 INFO - Source dirs: /poc-sonar/src/main/java
15:52:36.087 INFO - Test dirs: /poc-sonar/src/test/java, /Volumes/Disk/Development/poc-sonar/src/test/groovy
15:52:36.088 INFO - Binary dirs: /poc-sonar/build/classes/main
这提出了第一个问题:Sonar是否必须查看已编译的测试类以进行分析?
进一步记录日志:
15:52:37.435 INFO - Sensor JaCoCoSensor...
15:52:37.445 INFO - Analysing /poc-sonar/build/jacoco/test.exec
15:52:37.546 INFO - No information about coverage per test.
15:52:37.548 INFO - Sensor JaCoCoSensor done: 113 ms
15:52:38.105 INFO - Execute decorators...
15:52:38.580 INFO - Store results in database
Jacoco已选择test.exec
文件,但报告"No information about coverage per test"
该日志声明是什么意思?声纳服务器实际上暴露了正确的覆盖范围!它是Sonar报告缺失测试成功的指标吗?获得单位缺少什么
Unit Tests Coverage
50,0%
50,0% line coverage
Unit test success
0 tests
完整的gradle build.script:
ext {
spockVersion = '0.7-groovy-2.0'
groovyVersion = '2.2.1'
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'
apply plugin: 'sonar-runner'
group = "poc"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
maven {
credentials {
username "${artifactoryUsername}"
password "${artifactoryPassword}"
}
url "${artifactoryContextUrl}"
}
}
dependencies {
testCompile "junit:junit-dep:4.11"
testCompile "org.codehaus.groovy:groovy-all:$groovyVersion"
testCompile "org.spockframework:spock-core:$spockVersion"
}
tasks.withType(Test) { task ->
jacoco {
destinationFile = file("$buildDir/jacoco/${task.name}.exec")
}
}
sonarRunner {
sonarProperties {
property 'sonar.projectName', rootProject.name
property 'sonar.projectDescription', rootProject.name
// sonar server and database
property "sonar.host.url", sonarHostUrl
property "sonar.jdbc.url", sonarJdbcUrl
//property "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
property "sonar.jdbc.username", sonarJdbcUsername
property "sonar.jdbc.password", sonarJdbcPassword
property 'sonar.sourceEncoding', 'UTF-8'
}
}
tasks.sonarRunner.dependsOn = []
答案 0 :(得分:17)
在最近的Sonar版本中,测试报告位置的Sonar属性已从sonar.surefire.reportsPath
重命名为sonar.junit.reportsPath
。因此,您可能必须手动设置后者。例如:
apply plugin: "sonar"
subprojects {
apply plugin: "java"
sonarRunner {
sonarProperties {
property "sonar.junit.reportsPath", test.reports.junitXml.destination
}
}
}