3

I am having some problems with running Instrumentation Tests in Android Studio 3.0.1 and Kotlin (I didn't have such problems with previous Android Studios and Java). I created this (very simple) class [reference] in androidTest package which looks like this:

@RunWith(AndroidJUnit4::class)

@LargeTest
class DiscoverActivityTest {

    @JvmField
    @Rule
    val mDiscoverActivityTestRule = ActivityTestRule<DiscoverActivity>(DiscoverActivity::class.java)

    @Test
    fun onViewLoadedShowDiscoverFragment() {
        onView(withId(R.id.discoverFragmentView)).check(matches(isDisplayed()))
    }
}

When I try to run the test, there are two problems. First, it wants to run the test in the console without opening an emulator or deploying the apk package to a device (as it is just a normal local unit test). Additionally, I get this error message as well:

Class not found: "com.site.application.discover.DiscoverActivityTest" Empty test suite.

I wanted to force an instrumentation runner in the Edit Configuration option (something like this solution for example) but there is no such option in the new Android Studio 3.0.1 (compared to 2.3 for instance)

Update:

here's my gradle file content:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.site.application"
        minSdkVersion 17
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions "default"

    productFlavors {

        dev {
            dimension "default"
            applicationIdSuffix '.dev'
            versionNameSuffix "-dev" 
            resValue "string", "app_name", "Application Dev"
            resConfigs "en", "xxhdpi"
        }

        mock {
            dimension "default"
            applicationIdSuffix '.mock'
            versionNameSuffix "-mock"
            resValue "string", "app_name", "Shazam Clone Mock"
        }

        prod {
            dimension "default"
            resValue "string", "app_name", "Shazam Clone"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
    implementation "com.android.support:cardview-v7:$rootProject.ext.supportLibraryVersion"
    implementation "com.android.support:design:$rootProject.ext.supportLibraryVersion"
    implementation "com.android.support:recyclerview-v7:$rootProject.ext.supportLibraryVersion"
    implementation "com.android.support:support-v4:$rootProject.ext.supportLibraryVersion"
    implementation "com.android.support.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
    implementation "com.android.support.constraint:constraint-layout:$rootProject.ext.constraintLayoutVersion"

    implementation "com.google.code.gson:gson:$rootProject.ext.gsonVersion"
    implementation "com.github.bumptech.glide:glide:$rootProject.ext.glideVersion"
    implementation "com.github.ybq:Android-SpinKit:$rootProject.ext.spinKitVersion"

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation "junit:junit:$rootProject.ext.junitVersion"
    testImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
    testImplementation "com.nhaarman:mockito-kotlin-kt1.1:1.5.0"

    androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
}

It is worth mentioning that if I create a new configuration under Android Instrumented Tests in Run/Debug Configurations, select this configuration and run the project using this configuration (Shift + F10), the instrumentation tests will run just fine (All of them together). But when I try to run the tests individually (using the little play icon beside each individual test method or test class), a new configuration of the type Unknown will be created named after the test itself (e.g. DiscoverActivityTest.onViewLoadedShowDiscoverFragment in the code above) and then the IDE tries to use that configuration to run the test; Hence the errors.

This is the example configuration that the IDE will create by itself

Any suggestions?

Thanks in advance

Final Update: Well after a fresh reinstall of Android Studio, the problem was solved. So I guess there was something wrong with the IDE's configuration or something.

roostaamir
  • 1,928
  • 5
  • 24
  • 51
  • Could you post your app `build.gradle` file for more insight on the test configuration. – ahasbini Jan 24 '18 at 09:02
  • @ahasbini I have updated my question with the gradle file. Thank you for investigation – roostaamir Jan 24 '18 at 16:44
  • 1
    Thanks, have you tried removing all of your Run/Debug Configurations for your tests and run them again? – ahasbini Jan 24 '18 at 16:50
  • I tried removing every configuration in the `Run/Debug Configurations` menu, but I still have the same problem. As I said, the IDE will use a new self-made configuration for each individual test and tries to run tests with that (I have no idea why). I will upload a picture to demonstrate this behavior better – roostaamir Jan 24 '18 at 17:04
  • Yes this is weird, this is creating a JUnit Test config instead of an Android Instrumented Test config based on the icon of the config I'm seeing. I've created an new Kotlin Project and just ran the test that comes with it, it succeeded on the emulator with me. Just want to note that even though the code is under `androidTest` source set, I was given the option to run the test as JUnit test instead of Android Instrumented Test. Here's pic of what I mean: https://imgur.com/a/xRwT5 – ahasbini Jan 24 '18 at 20:49
  • My Android Studio won't give me such option to choose the test runner. Let me reinstall my IDE and check again. Maybe the problem gets fixed that way. I will let you know what happens. Thank you. – roostaamir Jan 25 '18 at 11:12
  • 1
    Well, it turns out that after reinstalling android studio the problem was fixed. Thank you for your cooperation @ahasbini – roostaamir Jan 25 '18 at 20:33

1 Answers1

0
android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
}

Try to add "testInstrumentationRunner" in your build.gradle.