当我在测试中使用powermock时,如何配置gradle为声纳产生代码覆盖范围?我发现Jacoco不支持这一点。还有其他CodeCoverage插件可以使用PowerMock?
jacoco离线仪器是解决方案。
请参阅我的gradle build file
Build and run tests:
Linux:
$ ./gradlew
Windows:
$ gradlew
------------------------------------------
"""
apply plugin: 'java'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
// Project group and version
group 'com.abcd.jacocoTest'
version '1.0.0'
// JDK version source compatibility
sourceCompatibility = 1.8
// JDK version target compatibility
targetCompatibility = 1.8
configurations {
jacocoAnt
jacocoRuntime
}
task wrapper(type: Wrapper) {
gradleVersion = "4.5.1"
}
defaultTasks 'clean', 'test'
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"
}
}
repositories {
mavenCentral()
}
dependencies {
jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.1'
jacocoAgent group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.4'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.4'
testCompile group: 'org.powermock', name: 'powermock-api-easymock', version: '1.7.4'
}
test {
testLogging {
afterSuite { desc, result ->
if (!desc.parent) { // will match the outermost suite
println "Unit Tests: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
}
}
}
jacoco {
append = "false"
destinationFile = file("$buildDir/reports/jacoco/jacoco-sonar/jacoco-coverage.exec")
}
}
jacoco {
toolVersion = "0.8.0"
}
jacocoTestReport {
reports {
html.destination file("${buildDir}/reports/jacoco/jacocoHtml")
}
}
sonarqube {
properties {
property "sonar.projectName", 'JacocoTest'
property "sonar.host.url", "http://localhost:9000"
property "sonar.java.binaries", "${buildDir}/classes"
property "sonar.java.libraries", "**/*.jar"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.jacoco.reportPaths", "${buildDir}/reports/jacoco/jacoco-sonar/jacoco-coverage.exec"
}
}
task instrument(dependsOn: ['classes']) {
ext.outputDir = buildDir.path + '/reports/classes-instrumented'
doLast {
ant.taskdef(name: 'instrument',
classname: 'org.jacoco.ant.InstrumentTask',
classpath: configurations.jacocoAnt.asPath)
ant.instrument(destdir: outputDir) {
fileset(dir: sourceSets.main.output.classesDir)
}
}
}
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(instrument)) {
tasks.withType(Test) {
doFirst {
classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime
}
}
}
}
task report(dependsOn: ['instrument', 'test']) {
doLast {
ant.taskdef(name: 'report',
classname: 'org.jacoco.ant.ReportTask',
classpath: configurations.jacocoAnt.asPath)
ant.report() {
executiondata {
ant.file(file: buildDir.path + '/reports/jacoco/jacoco-sonar/jacoco-coverage.exec')
}
structure(name: 'Example') {
classfiles {
fileset(dir: sourceSets.main.output.classesDir)
}
sourcefiles {
fileset(dir: 'src/main/java')
}
}
html(destdir: buildDir.path + '/reports/jacoco')
}
}
}
在这里报告任务将创建项目的离线仪器文件。
最后,执行 Sonarqube任务。然后,您可以看到还包括Powermocked类的覆盖范围。
Execution command ./gradlew report sonarqube
然后去您的Sonarqube主机查看(Localhost:9000)。
您可以尝试使用https://github.com/powermock/powermock/powermock/wiki/wiki/code-cover/code-coverage-with--只要https://github.com/powermock/powermock/issues/727固定,jacoco就不会使PowerMock与Jacoco兼容,这将使PowerMock与Jacoco兼容。
另外,您可以使用其他模拟框架,例如e。G。jmockit。就我记得而言,这与jacoco兼容。
我辞职使用gradle和jacoco。现在,我正在使用Maven Cobertura PowerMock,一切都没有黑客攻击。为什么我要使用小牛肉?因为我找不到使用Gradle在Cobertura中生产XML代码覆盖范围报告的方法。