ContextWrapper.startInstrumentation() doesn't start Instrumentation tests

1.4k views Asked by At

Starting Instrumentation tests from an ABD shell works fine:

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

To execute theses tests on devices not connected to a computer, I try to execute these tests from an app (neither the target app nor the test app) using following code:

    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();
    }

Debugging retrieves following correct values:

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

Though startInstrumentation() returns with true, the tests won't be executed. Any ideas?

1

There are 1 answers

2
Matthias Krieg On

I found the problem. It's the second null parameter in startInstrumentation(). I changed the code to:

...
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();
}

To execute all tests contained in a (Java) package, use instead:

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