Sushant Somani
Sushant Somani

Reputation: 1500

Running Instrumented Test with arguments using command prompt

I've an InstrumentedTest

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext(String groupName) {
Context appContext = InstrumentationRegistry.getTargetContext();
        UiDevice device=UiDevice.getInstance(getInstrumentation());
...
...
  }
}

And I want to execute it using adb shell command. But I've to pass value of groupName parameter for the method useAppContext(String groupName)

I was using command

adb shell am instrument -w -r   -e debug false -e class 'com.<package_name>.ExampleInstrumentedTest' com.<package_name>.test/android.support.test.runner.AndroidJUnitRunner  

But how can I pass method parameters as arguments to the command running over command prompt?

Upvotes: 10

Views: 4484

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76649

one can pass arguments with adb am instrument:

adb push ./app/build/outputs/apk/debug/com.<package_name>-debug.apk /data/local/tmp/com.<package_name>.debug
adb shell pm install -t -r "/data/local/tmp/com.<package_name>.debug"
adb push ./app/build/outputs/apk/androidTest/debug/com.<package_name>-debug-androidTest.apk /data/local/tmp/com.<package_name>.debug.test
adb shell pm install -t -r "/data/local/tmp/com.<package_name>.debug.test"
adb shell am instrument -w -r -e debug true -e class 'com.<package_name>.ExampleInstrumentedTest' com.<package_name>.debug.test/android.support.test.runner.AndroidJUnitRunner
Waiting for application to come online: com.<package_name>.debug.test | com.<package_name>.debug
Connecting to com.<package_name>.debug

maybe better use a TestRule to mock parameter input.

one can also pass android.testInstrumentationRunnerArguments.class alike:

./gradlew app:connectedAndroidTest -P android.testInstrumentationRunnerArguments.class=com.<package_name>.ExampleInstrumentedTest#someMethodToTest

or pass further arguments alike

-Pandroid.testInstrumentationRunnerArguments.argument1=make_test_fail

while one can run whole groups of tests (eg. EspressoTest) alike:

./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest

see the Android Testing Blueprint.

Upvotes: 2

Ashvin solanki
Ashvin solanki

Reputation: 4809

Ref : How to pass an argument to an AndroidTestCase?

public class MyTestRunner extends InstrumentationTestRunner {

    public static String BAR;

    public void onCreate(Bundle arguments) {

        if (null != arguments) {    
            BAR = (String) arguments.get("foo"));
        }    
        super.onCreate(arguments);
    }
}

I added to Android.mk:

LOCAL_JAVA_LIBRARIES := android.test.runner

And to AndroidManifest.xml:

<instrumentation 
    android:name="com.example.MyTestRunner"
    android:targetPackage="com.example" />

Ran it using this command line:

adb shell am instrument -w -e foo the_value_of_bar com.example/com.example.MyTestRunner

Edit 2

This sounds like the Parameterised JUnit Test use-case.

Check out the brief tutorial here - note that you will need to be using JUnit4 and I'm not sure Android's testing framework is ready for that.

That said, JUnit4 is backward compatible to JUnit3 so in-theory it'll be possible to use JUnit4 annotations under the android test case runner with a bit of build path tomfoolery.

Upvotes: 2

Related Questions