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:以上參考了一位大佬的方法,但是鏈接找不到了。在此鳴謝。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Android多模块覆盖率,Android代码覆盖率初探—问题已解决!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: go语言 html 模板语法,go语言快
- 下一篇: html像素绘制文字,HTML5 - C