Android Espresso:“未找到测试”、“进程崩溃”

2024-03-21

当使用 Espresso 测试我的 Android 应用程序时,我注意到运行配置为All in Module运行时找不到测试All in Package成功了。

我创建了以下可重现的案例:

  • 使用向导使用 minSdk 8 和空 Activity 创建新的干净应用程序
  • 使用 espresso 依赖项等配置 gradle(见下文)
  • 创建Android测试Run Configuration有选项All in Module和一个与All in Package
  • 添加带有测试的类(见下文)
  • 运行与All in Package: 考试通过了
  • 运行与All in Module: 没有找到测试

跑步与All in Module直到几天前还工作得很好。我记得所做的更改是将 Android Studio 升级到 1.4 并升级Android Support Library至 23.1.0 和Android Support Repository到 24.0.0。

顺便提一句。还gradlew connectedCheck and gradlew createDebugAndroidTestCoverageReport现在失败了。

有人有解决这个问题的建议吗?

应用程序的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.xyz.apptest"
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }
    androidTestCompile('com.android.support.test.espresso:espresso-idling-resource:2.2.1') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.4.1') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:rules:0.4.1') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'

    }
}

测试.java

@RunWith(AndroidJUnit4.class)
public class Test1 {

    @Rule
    public ActivityTestRule<MainActivity> activityTestRule =
            new ActivityTestRule<>(MainActivity.class, true, false);

    @Before
    public void setUp() {
        activityTestRule.launchActivity(new Intent());
    }

    @After
    public void cleanUp() {
    }

    @Test
    public void test() {
    }
}

在运行窗口中跟踪:

Testing started at 12:14 ...
Waiting for device.
Target device: 3_2_API_10 [emulator-5554]
Uploading file
    local path: C:\Users\xyz\Documents\Development\AndroidStudio\AppTest\app\build\outputs\apk\app-debug.apk
    remote path: /data/local/tmp/com.example.xyz.apptest
No apk changes detected.
Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.xyz.apptest
Uploading file
    local path: C:\Users\xyz\Documents\Development\AndroidStudio\AppTest\app\build\outputs\apk\app-debug-androidTest-unaligned.apk
    remote path: /data/local/tmp/com.example.xyz.apptest.test
No apk changes detected.
Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop com.example.xyz.apptest.test
Running tests
Test running startedTest running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.

logcat:未发现易受影响的差异。


从基于 API 10 的模拟器切换到 API 17 解决了该问题。

尽管直到上周 API 10 都运行良好,但它已经变得不可预测。有时它会运行,但大多数情况下不会。删除单个测试方法可能会使其正常工作,而将其放回去可能会使其继续工作(或不工作)。尝试第五次运行测试可能会再次起作用......

更新2016-03-16: 增加 API 10 模拟器的资源使测试再次可用于 API 10...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Espresso:“未找到测试”、“进程崩溃” 的相关文章

随机推荐