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?
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.
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.
Use the command ./gradlew test to run all tests.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With