Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Android uses jacoco to count line coverage

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Android how to use jacoco statistical code line coverage", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to use jacoco statistical code line coverage" bar!

Text

Please configure carefully and patiently according to the following steps. Any errors in the middle will affect the generation of the final coverage file!

1. Project build.gradle

Introduce jacoco core dependencies into the build.gradle of the project:

, buildscript {repositories {, maven {url "https://oss.sonatype.org/content/repositories/snapshots"}} dependencies {, classpath 'com.android.tools.build:gradle:3.2.1' / / can be configured this tutorial must use more than 3.2 classpath" org.jacoco:org.jacoco.core:0.8.5 "}, 2, Jacoco-report.gradle

Create a new jacoco-report.gradle file in the root directory of the project, which mainly defines a Gradle task: jacocoCoverageTestReport. The code is as follows:

Apply plugin: 'jacoco'tasks.withType (Test) {jacoco.includeNoLocationClasses = true} ext {getFileFilter = {- > def jacocoSkipClasses = null if (project.hasProperty (' jacocoSkipClasses')) {jacocoSkipClasses = project.property ('jacocoSkipClasses')} / / ignore class file configuration def fileFilter = [' * / R.classrooms,'* / Renewable classrooms,'* / 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 () / / the directory where the class for testing coverage is located (subject to the directory where the project class is located) / / the directory where gradle2.3 class is located def coverageDebugTree = fileTree (dir: "$project.buildDir/intermediates/classes/coverageDebug") Excludes: fileFilter) / / the directory where gradle3.2 class is located def coverageDebugTreeNewGradle = fileTree (dir: "$project.buildDir/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter) def mainSrc = "$project.projectDir/src/main/java" / / sets the directory sourceDirectories = files ([mainSrc]) / / compatible gradle version classDirectories = files ([coverageDebugTree]) CoverageDebugTreeNewGradle]) / / the following path also needs to check executionData = fileTree (dir: project.buildDir, includes: ['jacoco/testCoverageDebugUnitTest.exec',' outputs/code-coverage/debugAndroidTest/connected/coverage.ec'])}

Pay attention to the location of the above comments, each configuration must carefully check whether the path is correct and exists!

3. Build.gradle of app/*module

Configure the jacoco task in the app or the build.gradle corresponding to a module that you need to count:

Introduce the newly created jacoco-report.gradle above

Add coverageDebug BuildType.

The code is as follows:

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' / / Unit test 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, Test case

Write the corresponding code test case under the src/test/ directory of the corresponding module that needs to be tested. It is recommended to use the Squaretest plug-in to generate the test case. Please search for it yourself. There are basically no pits, so I won't repeat them here.

5. Run task jacocoTestReport

In the Gradle task pane of Android Studio, find the project/module/Tasks/reporting/jacocoTestReport task and double-click to run it to generate a line coverage report.

5. View the report

Open the project/module/build/reports/jacoco/index.html file to view the line coverage of each code file.

After opening the coverage report in a browser, it is generally as follows:

6. Tricks: quickly increase code coverage

Ignore the classes with low coverage according to the coverage report, see the first comment in the step 2 code

, / / ignore class file configuration def fileFilter = ['* * / R.classrooms,'* * / Renewable classes.classrooms,'* / BuildConfig.*','* / Manifest*.*','* / * $ViewInjector*.*'// continue to add low coverage classes that you want to ignore'* / ClassA.class','**/ClassB.class',], thank you for reading The above is the content of "how Android uses jacoco to count line coverage". After the study of this article, I believe you have a deeper understanding of how Android uses jacoco to count line coverage, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report