I inherited an Android Project composed by the main app and a Library . My goal is to test the whole project, so I started from the Library : first i use JUnit to test some classes/method, writing my test undeer app/src/test/java (unit test) of my Library project, also generating a code coverage report by Android Studio. The problem comes when i find a Class that uses Parcel (from Android Framework), so this time I had to setup an instrumented test (app/src/androidTest/java), also within the Library project.
When I try run the instrumented test, plugging an Android device, i got this message:
Started running tests
Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
Empty test suite.
I don't understand why "Empty test suite" while there're several @Test methods in my test class. Have I to setup Gradle how to link main app and library during test o maybe I have to move all my test in the main app?!
This is my gradle for Library module:
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
// Added for instrumental tests
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
testCoverageEnabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "commons-io:commons-io:$rootProject.commonsioVersion"
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Rxjava2 and RxAndroid
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.okhttp:okhttp:2.7.0'
// Dependencies for Room
// Room (use 1.1.0-alpha1 for latest alpha)
implementation 'android.arch.persistence.room:runtime:1.1.1'
implementation 'android.arch.persistence.room:rxjava2:1.1.1'
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
implementation "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
implementation "org.apache.httpcomponents:httpcore:$rootProject.httpcoreVersion"
testImplementation 'junit:junit:4.12'
// // [Android Instrumented test]
androidTestImplementation 'junit:junit:4.12'
// androidTestImplementation 'androidx.test:runner:1.1.0'
// androidTestImplementation 'androidx.test:rules:1.1.0'
// // Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// // Optional -- UI testing with Espresso
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
// // Optional -- UI testing with UI Automator
// androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
api files('libs/lifesense_ble_v3.3.7.jar')
api files('libs/MedPixel.jar')
}
and app's:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "it.*******.********"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 2
versionName "1.0.2"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
debuggable true
}
}
lintOptions {
checkReleaseBuilds false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation project(':..:Libraries:app')
implementation "com.android.support:support-v4:$rootProject.supportLibraryVersion"
implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
implementation "commons-io:commons-io:$rootProject.commonsioVersion"
// Dependencies for ButterKnife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// Dependencies for Dagger
implementation 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
// Dependencies for RxBinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
// Dependencies for Rxjava2 and RxAndroid
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
// Dependencies for Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-simplexml:2.3.0'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.okhttp:okhttp:2.7.0'
// Dependencies for Room
implementation 'android.arch.persistence.room:runtime:1.1.1'
// Dependencies for Roboelecttic
testImplementation 'org.robolectric:robolectric:3.6.1'
testImplementation 'junit:junit:4.12'
// Dependencies for Mockito
testImplementation 'org.mockito:mockito-core:2.7.22'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.squareup.retrofit2:retrofit-mock:2.0.0'
androidTestImplementation 'com.squareup.okhttp:mockwebserver:2.7.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'org.mockito:mockito-android:2.7.22'
}
configurations.all {
resolutionStrategy {
force "android.arch.core:runtime:1.1.1"
force "com.android.support:support-core-utils:28.0.0"
/*force "com.android.support:support-compat:28.0.0"*/
}
}
Thanks for the attention!
I have run into the same problem, not with Parcel
but with other android components like Context
.
For this issue,
Empty test suite
Your instrumented test class in must annotated with @RunWith(AndroidJUnit4.class)
.
Initially I did write the Instrumented tests in app
. But the problem with writing library's InstrumentationTests
in app
is, you will not get the coverage report for your library
.
So, I moved the InstrumentedTests
back to library/androidTest
folder. After a lot of research, I found a solution to get complete coverage report for library
Do following things in your library's build.gradle
file to get coverage
Add apply plugin: 'jacoco'
Enable test coverage as below
android {
buildTypes {
debug {
testCoverageEnabled true
}
}
...
}
Add below dependencies
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
I believe, for any module in your project, If there are androidTests
in your module/library, there will be a default task called combinedTestReportDebug
.
You can find it in Gradle
menu available in vertical bar at Top right corner of Android studio.
Gradle menu > :your-library > Tasks > reporting > combinedTestReportDebug
or
from run this command in your Terminal
> ./gradlew clean combinedTestReportDebug -p :your-library
After the tests are completed, you can find coverage or failed tests in following location
For failed tests: your-library/build/reports/androidTests/connected/index.html
For coverage report: your-library/build/reports/coverage/debug/index.html
I hope this helps.
Sorry, I have forgot to mention the below step which is needed because if not combinedTestReportDebug
will not be visible. Thanks to @nicoabie.
In your project's build.gradle
file, do the following to make combinedTestReportDebug
task visible in your library gradle tasks.
buildscript {
repositories {
jcenter()
}
dependencies {
...
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.13.0'
...
}
}
...
apply plugin: 'com.vanniktech.android.junit.jacoco'