Java判断离线,在离线检测模式下使用适用于Gradle的Jacoco插件测试覆盖率

我正在尝试使用Jacoco Gradle插件为我的PowerMockito单元测试获取代码覆盖率 . 我必须使用离线检测,因为PowerMockito不支持在线模式 .

对于Jacoco,您必须创建Ant任务,这将使代码覆盖,但离线检测的所有示例都使用与 'com.android.library' 插件不兼容的 'Java' 插件 .

使用'Java'插件的可行解决方案:

apply plugin: 'java'

apply plugin: 'jacoco'

//Additional SourceSets can be added to the jacocoOfflineSourceSets as needed by

project.ext.jacocoOfflineSourceSets = [ 'main' ]

task doJacocoOfflineInstrumentation(dependsOn: [ classes, project.configurations.jacocoAnt ]) {

inputs.files classes.outputs.files

File outputDir = new File(project.buildDir, 'instrumentedClasses')

outputs.dir outputDir

doFirst {

project.delete(outputDir)

ant.taskdef(

resource: 'org/jacoco/ant/antlib.xml',

classpath: project.configurations.jacocoAnt.asPath,

uri: 'jacoco'

)

def instrumented = false

jacocoOfflineSourceSets.each { sourceSetName ->

if (file(sourceSets[sourceSetName].output.classesDir).exists()) {

def instrumentedClassedDir = "${outputDir}/${sourceSetName}"

ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {

fileset(dir: sourceSets[sourceSetName].output.classesDir, includes: '**/*.class')

}

//Replace the classes dir in the test classpath with the instrumented one

sourceSets.test.runtimeClasspath -= files(sourceSets[sourceSetName].output.classesDir)

sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)

instrumented = true

}

}

if (instrumented) {

//Disable class verification based on https://github.com/jayway/powermock/issues/375

test.jvmArgs += '-noverify'

}

}

}

test.dependsOn doJacocoOfflineInstrumentation

对于我使用的Android

apply plugin: 'com.android.library'

而且这个插件没有 'classes' 任务, 'jacocoTestReport' 任务并且具有不同的SourceSet实现 . 当我尝试使用这两个插件时出现此错误消息:

The 'java' plugin has been applied, but it is not compatible with the Android plugins

所以,我的问题是:如果在Android平台的情况下如何使用Jacoco离线工具进行Gradle .

Java项目中集成JaCoCo进行代码覆盖率检查,可以通过以下步骤实现: 1. **添加依赖或插件**: - 对于Maven项目,在`pom.xml`文件中添加JaCoCo插件。例如: ```xml <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` - 对于Gradle项目,在`build.gradle`文件中添加JaCoCo插件。例如: ```groovy plugins { id 'jacoco' } ``` 2. **配置插件**: - 在Maven项目的`pom.xml`中,可以进一步配置插件的行为,如指定输出目录、设置覆盖率规则等。例如: ```xml <configuration> <append>true</append> <destFile>${basedir}/target/jacoco.exec</destFile> <dataFile>${basedir}/target/jacoco.exec</dataFile> <outputDirectory>${basedir}/target/jacoco-ut</outputDirectory> </configuration> ``` - 在Gradle项目中,可以在`build.gradle`中配置任务行为。例如: ```groovy jacocoTestReport { reports { xml.enabled = true html.enabled = true } } ``` 3. **运行测试并生成报告**: - 使用Maven命令行运行测试并生成覆盖率报告: ```sh mvn clean test jacoco:report ``` - 使用Gradle命令行运行测试并生成覆盖率报告: ```sh ./gradlew clean test jacocoTestReport ``` 4. **查看报告**: - 生成的HTML报告通常会放在`target/site/jacoco/index.html`(Maven)或`build/reports/jacoco/test/html`(Gradle)目录下,可以用浏览器打开查看详细的覆盖率信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值