android instrumentation测试用例 - getinstrumentation()返回null

Vel*_*elo 5 instrumentation android

我一直在尝试将测试用例扩展到intrutrumentationtestcase,每当我调用getinstrumentation()时,它都会返回一个Instrumentation的null实例而不是Instrumentation,从而使我想做的任何自动化变得毫无用处.我也在清单中设置了权限,即使我只是在同一个应用程序上测试自动化,这个案例将继续运行......任何想法?

Gal*_*lal 5

您需要通过injectInstrumentation(InstrumentationRegistry.getInstrumentation());使用InstrumentationRegistry官方Android testing-support-lib:0.1 调用来以编程方式注入 检测


小智 3

我遇到了类似的问题,似乎 getInstrumentation() 方法仅在调用基类 (InstrumentationTestCase) setUp 方法后才返回有效的检测。请查看下面的代码并检查 LogCat 调试消息:

import android.app.Instrumentation;
import android.test.InstrumentationTestCase;
import android.util.Log;

public class TestInstrumentation extends InstrumentationTestCase {

private static final String LOG_TAG = BrowseLocationsTest.class.getSimpleName();

private Instrumentation instr;

public TestInstrumentation() {
    instr = getInstrumentation();
    Log.d(LOG_TAG, "TestInstrumentation instrumentation: " + instr);
}

@Override
protected void setUp() throws Exception {
    instr = getInstrumentation();
    Log.d(LOG_TAG, "setUp instrumentation: " + instr);

    super.setUp();

    instr = getInstrumentation();
    Log.d(LOG_TAG, "setUp instrumentation: " + instr);
}

public void testInstrumentation() {
    Log.d(LOG_TAG, "testInstrumentation instrumentation: " + instr);
}
}
Run Code Online (Sandbox Code Playgroud)

在 super.setUp() 调用之后,检测就按预期就位。