在Android 4.4上运行Espresso测试?

问题描述 投票:1回答:2

我正在尝试将我们的应用程序迁移到使用Espresso进行UI测试,但我无法让Gradle使用运行Android 4.4的设备(API 19,我们的最低部署目标)找到我的测试。 Android 6.0(API 23)上的测试运行正常。我根据app/build.gradlethis将jUnit运行器和依赖项添加到this(由于模块之间的版本冲突,我排除了注释):

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

dependencies {
...
    androidTestCompile("com.android.support.test.espresso:espresso-core:2.2.2") {
        exclude module: 'support-annotations'
    }
    androidTestCompile("com.android.support.test:runner:0.5") {
        exclude module: 'support-annotations'
    }
    androidTestCompile("com.android.support.test:rules:0.5") {
        exclude module: 'support-annotations'
    }
}

然后我用一些模拟测试代码创建了所需的目录结构app/src/androidTest/java/和包com.companyname.appname以及java类EspressoTest.java

@RunWith(AndroidJUnit4.class)
public class EspressoTest {

    @Rule
    public ActivityTestRule<TermsOfUse> termsOfUseActivityTestRule = new ActivityTestRule<>(TermsOfUse.class);

    @Test
    public void iAmAtTouView() {
        onView(withId(R.id.terms_of_use_content)).check(matches(isDisplayed()));
    }
}

如果我右键单击测试类EspressoTest并选择“运行'EspressoTest'”,我会收到错误消息:

$ adb shell am instrument -w -r   -e package com.companyname.appname -e debug false com.companyname.appname.qa.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.

另外,如果我在命令行中输入./gradlew connectedAndroidTest,我会得到:

Starting 0 tests on GT-I9305 - 4.4.4
Tests on GT-I9305 - 4.4.4 failed: Instrumentation run failed due to 'Process crashed.'

com.android.builder.testing.ConnectedDevice > No tests found.[GT-I9305 - 4.4.4] FAILED 
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
:app:connectedAuditDebugAndroidTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:connectedAuditDebugAndroidTest'.
> There were failing tests. See the report at: 
...

所以对我来说,在尝试使用Android设备执行测试任务时,Gradle似乎无法识别我的测试类。我怎样才能解决这个问题?

android gradle junit4 android-espresso ui-testing
2个回答
0
投票

首先检查可用于gradle运行的任务:

gradle <app-module>:tasks

要么

gradle --gui

查看您正在运行的任务是否在任务列表中。您还试图运行哪台机器:MacOS还是Windows?如果不是至少从android工作室同步你的项目总是一个好主意,在运行测试之前,总是使用'​​sudo'运行gradle


0
投票

你有没有机会启用multidex,因为你的*-androidTest.apk包含超过64k的方法?

如果是这样,您需要确保第一个dex文件(classes.dex)包含您要运行的所有*Test类。您可以像这样配置:

android {
    defaultConfig {
        multiDexEnabled true
        multiDexKeepProguard file('multidex-config.pro')
    }
}

并在您的multidex-config.pro中为您的测试类添加常规的-keep规则,例如

-keep path.to.my.package.**.*Test { *; }

如果您的测试类具有您自己的包路径之外的依赖项,请务必将这些依赖项包含在单独的-keep语句中。在java.lang.ClassNotFoundException中寻找adb logcat来识别缺失的类别。

© www.soinside.com 2019 - 2024. All rights reserved.