ContextWrapper.startInstrumentation()不启动Instrumentation测试

时间:2015-06-10 12:15:48

标签: android android-instrumentation

ABD shell启动检测测试工作正常:

adb shell am instrument de.manayv.lotto.test/android.support.test.runner.AndroidJUnitRunner

要在未连接到计算机的设备上执行这些测试,我尝试使用以下代码从应用程序(目标应用程序和测试应用程序)执行这些测试:

    String packageName = "de.manayv.lotto.noonlinegambling";

    final List<InstrumentationInfo> list = getPackageManager().queryInstrumentation(
                                                                            packageName, 0);
    if  (list.isEmpty()) {
        Toast.makeText(this, "Cannot find instrumentation for " + packageName,
                       Toast.LENGTH_SHORT).show();
        return;
    }

    final InstrumentationInfo instrumentationInfo = list.get(0);
    final ComponentName componentName = new ComponentName(instrumentationInfo.packageName,
                                                          instrumentationInfo.name);

    if (!startInstrumentation(componentName, null, null)) {
        Toast.makeText(this, "Cannot run instrumentation for " + packageName,
                       Toast.LENGTH_SHORT).show();
    }

调试检索以下正确的值:

  instrumentationInfo.packageName = de.manayv.lotto.test
  instrumentationInfo.name = android.support.test.runner.AndroidJUnitRunner

虽然startInstrumentation()返回true,但测试不会被执行。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我发现了问题。它是startInstrumentation()中的第二个null参数。我将代码更改为:

...
Bundle arguments = new Bundle();
arguments.putString("class", "de.manayv.lotto.espresso.BalanceComputationTest");

if (!startInstrumentation(componentName, null, arguments)) {
    Toast.makeText(this, "Cannot run instrumentation for " + packageName,
                   Toast.LENGTH_SHORT).show();
}

要执行(Java)包中包含的所有测试,请改为使用:

arguments.putString("package", "de.manayv.lotto.espresso");