Home » Android » java – how to pass an argument to a android junit test (Parameterized tests)

java – how to pass an argument to a android junit test (Parameterized tests)

Posted by: admin June 15, 2020 Leave a comment


I am running my android junit tests using command line or eclipse depending if I am in dev mode or in device testing.

A java application has a Main (String[] args) method and it is easy to pass an argument to it (either with eclipse in the Arguments tab in the run configuration section, or by command line)

for an android junit test it is different, there is no Main method and there is nowhere I can set some arguments.

I have read here and there a solution would be to use a properties file. Is that the only way?
If you ave a quick and easy example, it would be very appreciated.


>>> Edit <<<

I am using Robotium, and the junit extends ActivityInstrumentationTestCase2
See below the very basic junit test:

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.jayway.android.robotium.solo.Solo;

public class Test_arg extends ActivityInstrumentationTestCase2 {    
    private static final String TARGET_PACKAGE_ID                = "com.myapp.test";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.myapp";
    private static Class        launcherActivityClass;
    private Solo solo;

    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);

    public Test_arg() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);


    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());

    public void tearDown() throws Exception {
        try {
        } catch (Throwable e) {

    public void test_01() throws InterruptedException { 
        Log.i("TEST", "test");   

and the android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="5" />

        android:targetPackage="com.slacker.radio" android:functionalTest="true"/>

        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
How to&Answers:

You can extend InstrumentationTestRunner and get the arguments in onCreate:

public class MyTestRunner extends InstrumentationTestRunner {

    private static final String TAG = null;
    private String mArgument;

    /* (non-Javadoc)
     * @see android.test.InstrumentationTestRunner#onCreate(android.os.Bundle)
    public void onCreate(Bundle arguments) {

        if (arguments != null) {
            mArgument = arguments.getString("myarg");

    public String getArgument() {
        return mArgument;


Add the instrumentation to AndroidManifest.xml, in your case:

        android:targetPackage="com.slacker.radio" android:functionalTest="true"/>

and in your Instrumentation (i.e ActivityInstrumentationTestCase2) tests you can do something like this:

public void testSomething() {
    MyTestRunner myTestRunner = (MyTestRunner)getInstrumentation();
    Log.d(TAG, "argument=" + myTestRunner.getArgument());

and get the arguments that you can specify in the command line as

$ adb shell am instrument -e myarg MYARG com.example.my.test/.MyTestRunner -w


In Setup function of the Class that extends ActivityInstrumentationTestCase2, add the following code:

MyTestRunner myTestRunner = (MyTestRunner)getInstrumentation();
String argument = myTestRunner.getArgument();
solo = new Solo(myTestRunner, getActivity()); 

Directory structure should be:


Then in the AndroidManifest.xml, set

<manifest package="package.x.y.z" ...


Invoke the above package with the command line:

adb shell am instrumentation -w package.x.y.z/package.x.y.z.test.CustomInstrumentationRunner