Android多模块覆盖率,Android代码覆盖率初探—问题已解决!

6月份到时候就要搞Android代码覆盖率,但是当时客户端即将改版,所以就耽搁了。在Android大哥的帮助下,终于完成了初步的代码覆盖率统计。

在github上找到了封装好的jacocoTestHelper:

gradle配置:

apply plugin: 'com.android.application'

apply plugin: 'jacoco'

def coverageSourceDirs = [

'../app/src/main/java'

]

android {

compileSdkVersion 25

buildToolsVersion "25.0.2"

defaultConfig {

applicationId "com.test.app"

minSdkVersion 18

targetSdkVersion 25

versionCode 37

versionName "1.8.20.5"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

debug {

testCoverageEnabled = true

}

release {

testCoverageEnabled = true

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

lintOptions{

checkReleaseBuilds false

abortOnError false

}

}

task jacocoTestReport(type: JacocoReport) {

group = "Reporting"

description = "Generate Jacoco coverage reports after running tests."

reports {

xml.enabled = true

html.enabled = true

}

classDirectories = fileTree(

dir: './build/intermediates/classes/debug',

excludes: ['**/R*.class',

'**/*$InjectAdapter.class',

'**/*$ModuleAdapter.class',

'**/*$ViewInjector*.class'

])

sourceDirectories = files(coverageSourceDirs)

executionData = files("$buildDir/outputs/code-coverage/connected/coverage.ec")

doFirst {

new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->

if (file.name.contains('$$')) {

file.renameTo(file.path.replace('$$', '$'))

}

}

}

}

dependencies {

compile 'com.github.Jay-Goo:JacocoTestHelper:v0.0.2'

}

现在仍存在问题:

手动执行时,执行结束按home退出,再重新打开app时,覆盖率统计可以叠加。如果用appium执行测试,每次open application时会重启app(我猜),重新调用JacocoHelper.init(),这样每执行一条都会生成一个coverage.ec文件,如何用多个ec文件生成覆盖率报告是未来要解决的问题,或者说,是否存在一种方法让appium open application的时候不进行杀掉重启。啊,任重道远啊。

另:最近发现用python直接写case并没有比用RF麻烦,而且执行速度感觉比RF快一点。RF的优点在于可以直观管理用例,android方面的关键字自己还是需要封装一些,现有的AppiumLibrary不能满足需求。

另2:此时特想搞个mbp,但是就以现在买买买的节奏,啥时候能攒下来钱哇(望天)

20170912

问题得以解决哈哈哈哈哈哈哈,稍晚更新解决方案,开心哈哈哈哈哈哈哈哈哈

20171101

更新:

appium每执行完一个case,点击两次回退退出app,将/mnt/sdcard/coverage.ec导出并重命名到指定路径(本文为$buildDir/outputs/code-coverage/connected/)

在gradle中增加:

task removeOldMergeEc(type: Delete) {

delete "$buildDir/outputs/code-coverage/connected/mergedcoverage.ec"

}

task mergeReport(type:JacocoMerge,dependsOn:removeOldMergeEc){

group = "Reporting"

description = "merge jacoco report."

destinationFile= file("$buildDir/outputs/code-coverage/connected/mergedcoverage.ec")

//"$buildDir/outputs/code-coverage/connected/"为存放.ec文件的路径

FileTree tree = fileTree("$buildDir/outputs/code-coverage/connected/") {

include '**/*.ec'

}

executionData = tree

}

并将上文task jacocoTestReport(type: JacocoReport)中以下代码进行修改:

executionData = files("$buildDir/outputs/code-coverage/connected/coverage.ec)

改为:

executionData = files("$buildDir/outputs/code-coverage/connected/mergedcoverage.ec")

所有case执行结束后,执行

gradlew mergeReport -Pec_dir="F:\Mox\Mox\mox-portal\build\outputs\code-coverage\connected"

确认"$buildDir/outputs/code-coverage/connected/路径下已生成mergedcoverage.ec后

gradlew jacocoTestReport

然后就可以开心的查看报告啦~

PS:以上参考了一位大佬的方法,但是链接找不到了。在此鸣谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值