Instrumentation测试将作为Android Studio 3上的本地单元测试运行(空测试套件)

时间:2018-01-23 11:39:31

标签: java android unit-testing android-studio kotlin

我在Android Studio 3.0.1和Kotlin中运行Instrumentation Tests时遇到了一些问题(我之前的Android工作室和Java没有遇到过这样的问题)。我在androidTest包中创建了这个(非常简单的)类[reference],如下所示:

@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()))
    }
}

当我尝试运行测试时,有两个问题。首先,它希望在控制台中运行测试而无需打开模拟器或将apk包部署到设备(因为它只是一个正常的本地单元测试)。此外,我也收到此错误消息:

  

未找到班级:   “com.site.application.discover.DiscoverActivityTest”   空测试套件。

我想在Edit Configuration选项中强制使用一个检测运行器(例如this solution之类的选项),但新Android Studio 3.0.1中没有这样的选项(例如与2.3相比)

更新:

这是我的gradle文件内容:

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"
}

值得一提的是,如果我在Android Instrumented Tests的{​​{1}}下创建一个新配置,选择此配置并使用此配置运行项目(Shift + F10),仪表测试将运行正常(所有这些在一起)。但是当我尝试单独运行测试时(使用每个单独测试方法或测试类旁边的小游戏图标),将创建一个以测试本身命名的Run/Debug Configurations类型的新配置(例如{{1}在上面的代码中)然后IDE尝试使用该配置来运行测试;因此错误。

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

有什么建议吗?

提前致谢

最终更新fresh reinstall Android Studio之后,问题就解决了。所以我猜IDE的配置有问题。

1 个答案:

答案 0 :(得分:0)

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

尝试在build.gradle中添加“testInstrumentationRunner”。