11

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?

selalerer
  • 3,766
  • 2
  • 23
  • 33

3 Answers3

28

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"
Travis
  • 1,926
  • 1
  • 19
  • 26
  • 5
    This answer is much simpler and clearer than accepted one. – Mateusz Wlodarczyk Jun 15 '18 at 07:34
  • 1
    In addition if running from the am command simply add the parameter via -e. "am instrument -w -e exampleArgument hello -e class my.package.com.Class#testcase theapk.test/android.support.test.runner.AndroidJUnitRunner" – James DeRagon Dec 04 '18 at 19:20
  • InstrumentationRegistry help so much, i misunderstanding because of the answer with param in onCreat function – Nam Nguyễn Nov 07 '19 at 09:02
14

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.

selalerer
  • 3,766
  • 2
  • 23
  • 33
  • How do you handle values with spaces? – htellez Feb 09 '16 at 00:07
  • 1
    @htellez That doesn't really has anything to do with Android or adb. Each shell has its ways to handle values with spaces. If you're using Bash just wrap the value with a single quote. E.g. `'a value with spaces'`. – selalerer Feb 09 '16 at 09:44
  • 1
    I have been handling them as with "double\ quotes\ and\ slashes" (or 'double\ quotes\ and\ slashes') and it works fine, but it seems like an overkill wrapping with quotes AND escaping spaces. Removing the quotes or removing the escaping will result in disaster. I was wondering if there was a cleaner approach. – htellez Feb 11 '16 at 17:43
  • adb shell is shell by default (not bash), I'm not so familiar with shell, is this how it is supposed to work in shell? – htellez Feb 11 '16 at 17:49
  • Hello @selalerer how would you do it with out extending instrumentationTestRunner? my test just use "@"rule and don't have any extension at all. thank you – Loebre Feb 21 '17 at 17:24
0

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.

BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • My question was very android specific. I don't see how using Parameterised JUnit can help me give testers or automated testing the ability to pass this argument to my test. – selalerer Mar 04 '13 at 19:38
  • Well it gives you a mechanism to pass a parameter to a unit test. The implementation of the @Parameters method is how your users will be able to pass through an argument. Here you could provide a parser for reading SSIDs out of a test user's XML config file listing their SSIDs to test or access a database to list all the SSIDs that should be tested. – BrantApps Mar 04 '13 at 20:33
  • xml file or a DB for just one parameter seems like an overkill. I just wanted them to add it to the command line when running the test and found a way to do it. – selalerer Mar 05 '13 at 08:00
  • Sure thing. You should accept your answer as the correct one if you feel this is done and dusted. – BrantApps Mar 05 '13 at 09:22