安卓Espresso:"未找到测试","进程崩溃"

4
当使用Espresso测试我的Android应用程序时,我注意到配置为All in Module的AndroidTest找不到测试,而配置为All in Package则成功。
我创建了以下可重现的案例:
  • 使用向导创建具有minSdk 8和空Activity的新干净应用程序
  • 使用espresso依赖项等配置gradle(见下文)
  • 创建带有选项All in ModuleAll in Package的AndroidTest Run Configuration
  • 添加包含测试的类(见下文)
  • 使用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 connectedCheckgradlew createDebugAndroidTestCoverageReport现在也失败了。
有人有解决此问题的建议吗?
app的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'

    }
}

Test.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:未发现可疑差异。


我也遇到了同样的情况。在一个模块中运行所有测试用例是否可能? - thedarkpassenger
@thedarkpassenger 请看下面的答案。我增加了模拟器中的内存,突然它就可以工作了...模块中定义的所有测试都被执行了。 - passerby
1个回答

2

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

虽然直到上周API 10运行良好,但现在变得不可预测。有时它会运行,大多数情况下不会。删除单个测试方法可能会使其起作用,将其放回可能会使其继续工作(或者不工作)。连续尝试五次运行测试可能会再次使其起作用……

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接