Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Thanks

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

    }

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

    @Override
    public void tearDown() throws Exception {
        try {
            solo.finishOpenedActivities();
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        super.tearDown();
    }

    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"
    package="com.myapp.test"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="5" />

    <instrumentation        
        android:name="android.test.InstrumentationTestRunner"                      
        android:targetPackage="com.slacker.radio" android:functionalTest="true"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>
</manifest>
like image 258
Franck Avatar asked Feb 11 '13 20:02

Franck


People also ask

Does JUnit support parameterized tests?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized.

How is a parameterized test annotated in JUnit?

It has a single constructor that contains the test data. It has a static method which annotated with @parameters annotation and generates and returns test data. Each test data is used as a parameter for the test method. It needs a test method that is annotated with @test annotation.

Is it possible to pass command line arguments to a test execution in JUnit?

Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.


1 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)
     */
    @Override
    public void onCreate(Bundle arguments) {
        super.onCreate(arguments);

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

    public String getArgument() {
        return mArgument;
    }

}

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

<instrumentation        
        android:name="com.example.my.test.MyTestRunner"                      
        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
like image 163
Diego Torres Milano Avatar answered Sep 19 '22 16:09

Diego Torres Milano