0

只要我指定类,我就可以运行仪器测试。

adb shell am instrument -w -e class com.application.instrumentation.BaseActivityTest com.application.debug.test/android.support.test.runner.AndroidJUnitRunner

com.application.instrumentation.BaseActivityTest:.

Time: 2.204

OK (1 test)

我所有的仪器测试都位于com.application.instrumentation包中。当我尝试运行此包中的所有测试时,仪器无法找到任何测试。

adb shell am instrument -w -r -e package com.application.instrumentation com.application.debug.test/android.support.test.runner.AndroidJUnitRunner
INSTRUMENTATION_RESULT: stream=

Time: 0

OK (0 tests)


INSTRUMENTATION_CODE: -1

同样AndroidStudio- 我能够在特定类而不是整个包中运行测试。

BaseActivityTest.java

@RunWith(AndroidJUnit4.class)
@LargeTest
public class BaseActivityTest
            extends ActivityInstrumentationTestCase2<BaseActivity> {

    private BaseActivity mActivity;

    public BaseActivityTest() {
        super(BaseActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void checkPreconditions() {
        assertThat(mActivity, notNullValue());
        // Check that Instrumentation was correctly injected in setUp()
        assertThat(getInstrumentation(), notNullValue());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

}

AndroidStudio版本是1.2.2.

测试依赖:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

可用仪器:

adb shell pm list instrumentation
instrumentation:com.application.debug.test/android.support.test.runner.AndroidJUnitRunner (target=com.application.debug)
4

1 回答 1

1

我相信这是因为 JUnit4 风格的测试类不应该扩展ActivityInstrumentationTestCase2<BaseActivity>. 简单地省略那部分并且不扩展任何类,参见例如这个例子

于 2015-10-28T16:30:29.333 回答