I want to generate unit and integration test coverage html report for my Android project. This project is composed of 3 modules:
app module (I don't really want to make a report for this module)
sdk module (real target of the tests)
sdk-integration-tests (containing the sdk module integration tests)
I am following exactly this implementation
https://blog.mindorks.com/generate-global-code-coverage-report-in-android-development-using-jacoco-plugin
All tests pass.
But:
The report displays 0% of coverage. It does not match the actual test coverage. Classes and methods widely used in the tests are still marked as not covered.
I would like to merge the reports of two modules (sdk and sdk-integration-tests)
EDIT: One important remark: the tests use Mockito and Robolectric. I really think it could play an important part in the erroneous analysis of code coverage.
Do you have any idea?
Could you share your junitJacoco configuration?
Last time I also face the same issue, until find the solution here:
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
Related
I'm configuring an Android multi-module Gradle project that uses Kotlin for both the app AND the Gradle build files (gradle.build.kts).
I'm using Gradle 7.3.3.
First I add the Jacoco plugin to the module-level build.gradle.kts:
...
plugins {
...
jacoco
}
...
Then I click the icon in Android Studio to "sync project with gradle files."
Next, I find the debug build type and add this:
isTestCoverageEnabled = true
When I subsequently run ./gradlew testDebugUnitTest, a file is generated in the module at <MODULE>/build/outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec.
If, however, I don't add the line isTestCoverageEnabled = true, or if I set isTestCoverageEnabled = false, a coverage results file is generated in the module at <MODULE>/build/jacoco/testDebugUnitTest.exec.
When I generate each .exec file's HTML report, the "Total" rows at the bottom have matching counts.
Are these two files equivalent, but just located in different directories depending on the value of isTestCoverageEnabled?
If so, it seems that, as long as jacoco is included in the plugins, coverage results are generated regardless of whether the isTestCoverageEnabled = true line is added. Does isTestCoverageEnabled do anything else besides change the output directory? What am I missing?
I have struggled with this issue for 3 years across multiple projects, but I have a solution (or rather someone who resolved my issues).
The Android-Root-Coverage-Plugin can be used to combine both jUnit and instrumented tests in a Java and/or Kotlin multi module project without any great need to configure anything else. It resolved my following issues:
Getting coverage in multi module project
Incorrect coverage in Kotlin
Combining both jUnit & Instrumented test coverage
It has been confirmed that the cause of my issues were due to a bug in the android gradle plugin and as of Aug, 2022 it has yet to be resolved.
Specific to your question, isTestCoverageEnabled is used to configure the embedded JaCoCo in the android gradle plugin, which as stated above, is known to be buggy. Using the JaCoCo plugin can be configured in the gradle file but does not utilise the embedded JaCoCo in AGP
I have written test cases using JUnit and PowerMockito. IntelliJ IDEA is showing 100% coverage for lot many classes but Sonar is showing just 19%
I am using following versions:
PowerMockito: 2.0.2
JUnit: 4.12
Jacoco: 0.8.5
Sonar: 7.3
i have gone through multiple posts but nothing solved this issue.
This sounds like you're running into the issue that Powermock replaces the Jacoco annotation during its instrumentation, so Jacoco has nothing to report against. You may be able to change the instrumentation type to overcome this, but it's currently a know limitation when using Powermock.
I am using sonarqube in my android project to ensure code quality. I have also integrated jacoco to ensure test coverage of the code. But I want some classes should be ignored for checking test coverage while reporting by sonarqube, how can I accomplish that?
What are the android specific gradle properties for this ?
I am working on a project where in we are trying to use the ANDROID TEST ORCHESTRATOR for it's obvious benefits of isolating crashes. But while executing the test suite, it appears to me that as the orchestrator initiates a new process for every test case, the test coverage report for the suite execution is always showing incomplete data (mostly the data for the last test case present in the test suite).
So I was wondering that is there a way to overcome this problem and generate a jacoco code coverage report for all instrumented tests existing in the test suite.
If you're using the android test orchestrator this is the problem. There is an open bug report: https://issuetracker.google.com/issues/72758547
The only solution that I know of is to disable the android test orchestration until a fix is released.
In my Android Java build.gradle I had to comment out the test orchestrator like so:
android {
...
testOptions {
// temporarily disable the orchestrator as this breaks coverage: https://issuetracker.google.com/issues/72758547
//execution 'ANDROID_TEST_ORCHESTRATOR'
...
}
}
The issue mentioned above is now fixed and the latest gradle version is working fine for all android devices.
I have read around, there are a number of extensive answers (like this one) but the Android world evolves so fast that they seem to be a bit outdated and the official documentation still refers to Eclipse with ADT.
I am running AS 1.1 and I am trying to setup simple junit tests to run on the emulator, without Robolectric. If I don't include junit in my build.gradle, it can't find #After, #Before and #Test and I get package org.junit does not exist. Upon adding
// unit tests
androidTestCompile 'junit:junit:4.11'
the error becomes
Error:duplicate files during packaging of APK
[...]/app/build/outputs/apk/app-debug-test-unaligned.apk
Path in archive: LICENSE.txt
Origin 1: [...]/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
Origin 2: [...]/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'LICENSE.txt'
}
}
Following the console suggestion of excluding LICENSE.txt, it then works but it feels like a hack. So I'm wondering, am I maybe missing something? Thanks.
Android Studio unit testing support comes in 1.1 Beta 4 (release announcement) with Gradle plugin version 1.1.0-rc1.
More info in official document.
However it is experimental feature for now. E.g. it breaks installDebug gradle task.
For using JUnit in instrumentation tests there is good guide for Espresso library and another covering new AndroidJUnitRunner.
If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.
https://github.com/hitherejoe/Android-Boilerplate