Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 752
Sushant Somani Avatar asked Sep 24 '18 07:09

Sushant Somani


People also ask

How to run Android tests from command line?

To run a test from the command line, run adb shell to start a command line shell on your device or emulator. Inside that shell you can interact with the activity manager using the am command and use its instrument subcommand to run your tests.

What is the command for running test scripts?

In the Project Path field, enter the path to the directory that contains the application-under-test job on the test target machine. For example, enter. c:\cmd-line-scripts. Click Go.

How do I run a gradle test in CMD?

Use the command ./gradlew test to run all tests.


1 Answers

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.

like image 61
Ashvin solanki Avatar answered Oct 24 '22 10:10

Ashvin solanki