Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an argument to an AndroidTestCase?

I've implemented an Instrumentation and an AndroidTestCase.

For my tests I need to connect to an external WIFI device. I want the testers to be able to specify an SSID for the test to use.

Giving the the command line (adb shell am instrument ...) to run the test is not a problem, but how can I add the SSID to the command line and extract it in the code?

like image 336
selalerer Avatar asked Mar 03 '13 17:03

selalerer


2 Answers

To expand on selalerer's answer, instrumentation test(s) can be started with arguments specified via Gradle:

./gradlew -Pandroid.testInstrumentationRunnerArguments.exampleArgument=hello connectedAndroidTest

You can retrieve instrumentation arguments using:

InstrumentationRegistry.getArguments().getString("exampleArgument") // returns "hello"
like image 52
Travis Avatar answered Nov 19 '22 16:11

Travis


Found a solution.

I made my test-runner inherit from InstrumentationTestRunner and took the extra data in onCreate():

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

I was able to get the 'foo' parameter from the command line and use BAR in my AndroidTestCase.

like image 12
selalerer Avatar answered Nov 19 '22 15:11

selalerer