roostaamir
roostaamir

Reputation: 1968

Instrumentation Tests will run as Local Unit tests on Android Studio 3 (Empty test suite)

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.

Upvotes: 3

Views: 1482

Answers (1)

Huang Jianan
Huang Jianan

Reputation: 36

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

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

Upvotes: 0

Related Questions