androidkotlingradleandroid-espressoinstrumented-test

Android Studio can not find instrumented tests


I have an app with two flavour versions (pro and light) and I want to run instrumented tests. So I add two extra directories androidTestLight and androidTestPro (have three test folders androidTest, androidTestLight, androidTestPro). When I try to run a simple instrument test:

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.xxxx.xxxx.pro", appContext.packageName)
    }
}  

I get this information:

12/19 xx:xx:xx : Launching 'ExampleInstrumentedTest' on Samsung Galaxy Tab A7 Lite API 31.
Install successfully finished in 2 s 449 ms.
Running tests

but the test didn't run because android studio shows 0/0 tests.

I use following configuration: Android Studio Dolphin | 2021.3.1 Patch 1 gradle 7.0.4 compileSdkVersion 31 buildToolVersion 30.0.3 minSdkVersion 29

In the build.gradle I add in the defaultConfig section

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

and inside the dependencies I add

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    debugImplementation "androidx.fragment:fragment-testing:1.5.5"
    testImplementation "androidx.test:core-ktx:1.5.0"
    testImplementation "org.robolectric:robolectric:4.8"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.4"
    androidTestImplementation "androidx.test:rules:1.5.0"
    androidTestImplementation "androidx.test:runner:1.5.1"
    androidTestImplementation "androidx.test.uiautomator:uiautomator-v18:2.2.0-alpha1"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.1.0"

Simple unit tests run without a problem.


Solution

  • You are not using androidx in defaultConfig , Try this

    defaultConfig {
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }