Android使用acoco統(tǒng)計代碼行覆蓋率介紹
前言
jacoco是Java Code Coverage的縮寫,是Java代碼覆蓋率統(tǒng)計的主流工具之一。關(guān)于jacoco的原理介紹,在網(wǎng)上有很多文章,感興趣的同學(xué)可以去找別的博客看看,這里不做贅述。
最近接到這個需求,需要提升代碼單測覆蓋率并統(tǒng)計上傳,了解到的實現(xiàn)方式是jacoco+Squaretest插件,在網(wǎng)上查了不少的資料,不得不說網(wǎng)上大部分的資料都非常老了,gradle插件一般都是2.3的,導(dǎo)致很多類文件路徑錯誤,浪費了我很多時間,就算有比較新的博文,也大都是需要安裝運行之后從手機存儲再提取相應(yīng)的ec文件來執(zhí)行分析才能得出結(jié)果,我們這邊目標(biāo)是有腳本直接可以執(zhí)行獲取相應(yīng)的文件來統(tǒng)計并自動上傳,所以那種需要運行之后再提取相應(yīng)的文件解析不是很方便,而且這種方案在不同手機上可能還會帶來各種問題,于是,在我經(jīng)過一番實踐后終于實現(xiàn)了無需運行只需執(zhí)行g(shù)radle task便可得到覆蓋率文件,決定分享一下,為日后有需求的同學(xué)節(jié)省一些時間!
正文
請根據(jù)以下步驟細(xì)心耐心進(jìn)行配置,中間如果出現(xiàn)任何錯誤都會影響到最后覆蓋率文件的生成!
1、項目 build.gradle
在項目的 build.gradle
中引入 jacoco core
依賴:
、、、 buildscript { repositories { 、、、 maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } dependencies { 、、、 classpath 'com.android.tools.build:gradle:3.2.1' //可具體配置 本教程務(wù)必使用3.2以上 classpath "org.jacoco:org.jacoco.core:0.8.5" } } 、、、
2、jacoco-report.gradle
在項目根目錄新建一個 jacoco-report.gradle
文件,其中主要定義了一個 Gradle 任務(wù):jacocoCoverageTestReport。代碼如下:
apply plugin: 'jacoco' tasks.withType(Test) { jacoco.includeNoLocationClasses = true } ext { getFileFilter = { -> def jacocoSkipClasses = null if (project.hasProperty('jacocoSkipClasses')) { jacocoSkipClasses = project.property('jacocoSkipClasses') } //忽略類文件配置 def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*$ViewInjector*.*'] if (jacocoSkipClasses != null) { fileFilter.addAll(jacocoSkipClasses) } return fileFilter } } task jacocoTestReport(type: JacocoReport, dependsOn: ['testCoverageDebugUnitTest', 'createCoverageDebugCoverageReport']) { group = "Reporting" description = "Generate Jacoco coverage reports" reports { xml { enabled = true xml.destination file("build/reports/jacoco/jacoco.xml") } html { enabled = true html.destination file("build/reports/jacoco") } } def fileFilter = project.getFileFilter() //檢測覆蓋率的class所在目錄(以項目class所在目錄為準(zhǔn)) //gradle2.3 class所在目錄 def coverageDebugTree = fileTree(dir: "$project.buildDir/intermediates/classes/coverageDebug", excludes: fileFilter) //gradle3.2 class所在目錄 def coverageDebugTreeNewGradle = fileTree(dir: "$project.buildDir/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter) def mainSrc = "$project.projectDir/src/main/java" //設(shè)置需要檢測覆蓋率的目錄 sourceDirectories = files([mainSrc]) //兼容gradle版本 classDirectories = files([coverageDebugTree, coverageDebugTreeNewGradle]) //以下路徑也需要檢查 executionData = fileTree(dir: project.buildDir, includes: [ 'jacoco/testCoverageDebugUnitTest.exec', 'outputs/code-coverage/debugAndroidTest/connected/coverage.ec' ]) }
注意以上注釋的位置,每一個配置務(wù)必仔細(xì)檢查路徑是否正確且存在!
3、 app/*module的build.gradle
在你需要統(tǒng)計的 app或者某 module 對應(yīng)的 build.gradle
中進(jìn)行 jacoco 任務(wù)配置:
引入 上面新建的 jacoco-report.gradle
;
添加 coverageDebug BuildType
。
代碼如下:
apply plugin: 'com.android.library' apply from: '../jacoco-report.gradle' android { 、、、 defaultConfig { 、、、 testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } lintOptions { 、、、 abortOnError false } buildTypes { 、、、 coverageDebug { minifyEnabled false testCoverageEnabled true } } testOptions { unitTests.all { jvmArgs '-noverify' } unitTests { includeAndroidResources = true } unitTests.returnDefaultValues = true } 、、、 } dependencies { 、、、 testImplementation 'junit:junit:4.12' //單元測試 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' testImplementation 'org.robolectric:robolectric:4.3.1' testImplementation "org.robolectric:shadows-multidex:4.3" testImplementation 'org.hamcrest:hamcrest-all:1.3' // power mockito testImplementation 'org.mockito:mockito-core:2.8.9' testImplementation "org.powermock:powermock-api-mockito2:1.7.4" testImplementation "org.powermock:powermock-module-junit4:1.7.4" testImplementation "org.powermock:powermock-module-junit4-rule:1.7.4" testImplementation "org.powermock:powermock-classloading-xstream:1.7.4" }
4、 測試用例
在需要測試的對應(yīng) module 的 src/test/ 目錄下編寫對應(yīng)的代碼測試用例,建議使用 Squaretest
插件生成,使用方式請自行搜索,基本沒什么坑,這里不再贅述。
5、 運行 task jacocoTestReport
在 Android Studio 的 Gradle 任務(wù)窗格中,找到 project/module/Tasks/reporting/jacocoTestReport
這個任務(wù),雙擊運行,即可生成代碼行覆蓋率報告。
5、 查看報告
打開 project/module/build/reports/jacoco/index.html
文件,即可查看各個代碼文件的行覆蓋率。
覆蓋率報告用瀏覽器打開后一般如下:
6、 小花招:快速提升代碼覆蓋率
根據(jù)覆蓋率報告將覆蓋率低的類忽略,具體可查看步驟2代碼的第一個注釋處
、、、 //忽略類文件配置 def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*$ViewInjector*.*' //繼續(xù)添加想要被忽略的低覆蓋率的類 '**/ClassA.class','**/ClassB.class'、、、 ] 、、、
到此這篇關(guān)于Android使用acoco統(tǒng)計代碼行覆蓋率介紹的文章就介紹到這了,更多相關(guān)Android acoco統(tǒng)計代碼行覆蓋率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)自學(xué)筆記(一):Hello,world!
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(一):Hello,world!本文講解了創(chuàng)建HelloWorld工程、編寫代碼、啟動模擬器等步驟,需要的朋友可以參考下2015-04-04Android Studio下載更新Android SDK網(wǎng)絡(luò)異?;驘o法下載
這篇文章主要介紹了Android Studio下載更新Android SDK網(wǎng)絡(luò)異常或無法下載的相關(guān)資料,需要的朋友可以參考下2017-04-04Android NDK開發(fā)之:配置環(huán)境的詳解
本篇文章是對Android中的配置環(huán)境進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android組件ContextMenu實現(xiàn)長按事件
這篇文章主要為大家詳細(xì)介紹了Android組件ContextMenu實現(xiàn)長按事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04android基礎(chǔ)總結(jié)篇之一:Activity生命周期
本篇文章主要介紹了android基礎(chǔ)總結(jié)篇之一:Activity生命周期,想要學(xué)習(xí)的可以了解一下。2016-11-11