Android:具有代码覆盖率的本地测试?

Android: local tests with code coverage?

我想为我的 类 定义单元测试,并且正在 运行 解决以下问题。如果我在 androidTest 文件夹中定义单元测试(使用仪器进行测试),那么测试 运行s 通常会产生有效结果。但是,如果我在测试文件夹(本地测试)中定义测试,那么 运行 测试会生成一条消息 "class not found: "[classname]“空测试套件”。这不会那么糟糕,我只会 运行 来自 androidTest 的所有测试,除了我想使用代码覆盖和 Android Studio 不允许我 运行 来自androidTest,但仅来自测试。

为什么 Android Studio 无法在 test 文件夹中定义单元测试,但在 androidTest 文件夹中定义它们时能够找到它们?

代码:

public class SomeTest{

    private Context mInstrumentationCtx;

    @Before
    public void setup() {
        mInstrumentationCtx = InstrumentationRegistry.getTargetContext();
        // do some setup actions
    }

    @Test
    public void testFirst() throws Exception {
        Assert.assertEquals(true, true);
    }

}

Gradle 配置:

android {

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

debug {
            testCoverageEnabled = true
        }
}

testCompile 'junit:junit:4.12'
    testCompile ('com.android.support.test:runner:0.5', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

compile 'com.android.support:appcompat-v7:25.1.0'

本身可能不是答案

您可以尝试更改

 testCompile ('com.android.support.test:runner:0.5', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

这似乎是最近 gradle 版本中的默认运行程序。

我对后者有效,但对前者无效


编辑:

经过一番努力,目前看来降级到 2.2.3 版是 一个 解决方案,smth 一定在作者使用的 2.3 beta 中发生了变化