[JAVA] How to get coverage of Android Instrumented Tests [Do your best]

Introduction

This time, I will get coverage with Android Instrumented Tests, so make a note of it. First, when unit testing with Android Studio There was talk of using Robolectric to test Android-dependent parts on the JVM as well. Due to various reasons, I ended up writing the previous test Android Test.

I didn't know anything about Android development for the first time, so I had a good idea to investigate various things.

Strategy 1: Use "createDebugCoverageReport".

1. I couldn't change the version of Jacoco

The problem with this is that I couldn't get coverage for C0C1 because the version of Jacoco was old. Honestly, I don't think that the coverage of C0C1 is just numbers and it takes time to be honest. There is no such thing as "the higher the coverage, the higher the quality of the source code".

Well, if I was told to set it to 100, I had no choice but to do it, so I investigated various things. I don't write the test code to make it 100%, but give it to Jacoco 0.8.0 or higher. I saw the article saying that if you upgrade to Jacoco 0.8.0 version, it will improve C0C1 coverage. However, I couldn't change the version of Jacoco referenced in createDebugCoverageReport.

2. Exclusion settings cannot be made

This may be due to lack of study, but I couldn't set exclusions in createDebugCoverageReport. I did a lot of research, but I couldn't do it, so I can't help it.

From the viewpoints 1 and 2 above, we abandon Strategy 1 and move on to Strategy 2.

Strategy 2: "jacoco Test Report"

How to write your own task to get coverage in Gradle. With this, I was able to solve 1 and 2 of Strategy 1.

jacoco {
    toolVersion = "0.8.3"
}
task jacocoTestReport(type: JacocoReport, dependsOn: "connectedDebugAndroidTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

    // exclude auto-generated classes and tests
    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/R.styleable',
                      '**/R$*.styleable',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*IScript*.*',
                      'android/**/*.*',
                      'androidx/**/*.*',
                      '**/*_Impl*',
                      '**/*_Factory*',
                      '**/*_MembersInjector*',
                      '**/*Fake*']

    def debugTree = fileTree(dir:
            "${project.buildDir}/intermediates/javac/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])

    executionData = fileTree(dir: project.projectDir, includes:
            ['**/*.exec', '**/*.ec'])

    reports {
        xml.enabled = true
        xml.destination = "${buildDir}/jacocoTestReport.xml"
        csv.enabled = false
        html.enabled = true
        html.destination = "${buildDir}/reports/jacoco"
    }
}

I was able to specify the version of Jacoco and set exclusions like this, Let's use this one in the future.

Bye bye

Recommended Posts

How to get coverage of Android Instrumented Tests [Do your best]
How to get today's day of the week
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
As of April 2018 How to get Java 8 on Mac
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Rails] How to get the contents of strong parameters
[Swift] How to get the document ID of Firebase
An unsupported Java version How to get rid of errors
How to get values in real time with TextWatcher (Android)
How to get the longest information from Twitter as of 12/12/2016
How to implement one-line display of TextView in Android development
[jsoup] How to get the full amount of a document
How to batch run JUnit and get coverage as well
You can do anything with your Android smartphone or tablet! Summary of Arekore to move with Termux
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
[Swift UI] How to get the startup status of the application [iOS]
How to get the length of an audio file in java
How to get JDK 11 on your mac in a comfortable way
How to use Java HttpClient (Get)
How to get started with slim
How to "hollow" View on Android
Get JUnit code coverage on Android.
How to get parameters in Spark
How to use setDefaultCloseOperation () of JFrame
How to get along with Rails
[Android] How to make Dialog Fragment
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
[Java] How to get to the front of a specific string using the String class
How to get the absolute path of a directory running in Java
[For Rails beginners] Summary of how to use RSpec (get an overview)
Android development, how to check null in the value of JSON object
How to create your own annotation in Java and get the value