首页 > 解决方案 > 无法使用 com.android.test 模块插件运行 android espresso 测试

问题描述

我正在尝试分离一些 espresso 测试,使用com.android.test将作为功能测试运行的测试插件,因此不需要模拟,我只想使用 Espresso 和 uiautomator 进行一些 UI 自动化。

我已经用最简单的测试用例配置了测试项目,只是为了检查我的主要活动是否已启动:

@RunWith(AndroidJUnit4::class)
@LargeTest
class LoginTest {
    @Rule
    @JvmField
    var mActivityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

    @Test
    fun test() {
        Thread.sleep(15 * 1000)
     }
}

但是在活动启动后它会崩溃,并给出以下错误:

E/MonitoringInstr: Exception encountered by: com.****.****ui.main.MainActivity@9c9faca. Dumping thread state to outputs and pining for the fjords.
    android.content.res.Resources$NotFoundException: Drawable com.****.****/change_chart_icons_size with resource ID #0x7f070059

这很奇怪,原因有两个:1)主应用程序找不到资源ID,它在那里(应用程序在活动测试规则之外正常运行)。并且 2) 该资源不是可绘制资源,而是维度资源。所以没有意义...

这是我的build.gradle文件:

apply plugin: 'com.android.test'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'


android {
    compileSdkVersion versions.target_sdk

    defaultConfig {
        minSdkVersion versions.min_sdk
        targetSdkVersion versions.target_sdk

        testApplicationId 'com.****.****.test'

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    targetProjectPath ':mobile'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    implementation "com.google.dagger:dagger:$versions.dagger_version"
    implementation "com.google.dagger:dagger-android:$versions.dagger_version"
    implementation "com.google.dagger:dagger-android-support:$versions.dagger_version"
    implementation "com.android.support:support-v4:$versions.support"
    implementation "com.android.support:appcompat-v7:$versions.support"
    implementation "com.android.support:percent:$versions.support"
    implementation "com.android.support:design:$versions.support"
    implementation "com.android.support:cardview-v7:$versions.support"
    implementation "com.android.support:recyclerview-v7:$versions.support"
    implementation 'com.android.support.test:runner:1.0.2'
    implementation 'com.android.support.test:rules:1.0.2'
    implementation "org.mockito:mockito-android:$versions.mockito"
    implementation "com.nhaarman:mockito-kotlin:1.1.0"
    implementation('com.android.support.test.espresso:espresso-core:3.0.2')
    implementation('com.android.support.test.espresso:espresso-intents:3.0.2')
    implementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    implementation 'xmlwise:xmlwise:1.2.11'
}

我没有主意了,我不知道是不是因为我用匕首弄坏了;因为我已经放弃了com.android.support在新项目中实现模块测试的库的问题。

标签: androidandroid-espresso

解决方案


推荐阅读