使用命令提示符使用参数运行Instrumented Test

Sus*_*ani 9 android android-testing android-instrumentation

我有一个InstrumentedTest

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext(String groupName) {
Context appContext = InstrumentationRegistry.getTargetContext();
        UiDevice device=UiDevice.getInstance(getInstrumentation());
...
...
  }
}
Run Code Online (Sandbox Code Playgroud)

我想用adb shell命令执行它.但我要传递groupName方法的参数值useAppContext(String groupName)

我正在使用命令

adb shell am instrument -w -r   -e debug false -e class 'com.<package_name>.ExampleInstrumentedTest' com.<package_name>.test/android.support.test.runner.AndroidJUnitRunner  
Run Code Online (Sandbox Code Playgroud)

但是如何将方法参数作为参数传递给在命令提示符下运行的命令?

Ash*_*nki 2

参考: 如何将参数传递给 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我添加到Android.mk:

LOCAL_JAVA_LIBRARIES := android.test.runner
Run Code Online (Sandbox Code Playgroud)

以及 AndroidManifest.xml:

<instrumentation 
    android:name="com.example.MyTestRunner"
    android:targetPackage="com.example" />
Run Code Online (Sandbox Code Playgroud)

使用以下命令行运行它:

adb shell am instrument -w -e foo the_value_of_bar com.example/com.example.MyTestRunner
Run Code Online (Sandbox Code Playgroud)

编辑2

这听起来像参数化 JUnit 测试用例。

请查看此处的简短教程- 请注意,您将需要使用 JUnit4,但我不确定 Android 的测试框架是否已准备好。

也就是说,JUnit4 向后兼容 JUnit3,因此理论上可以在 android 测试用例运行器下使用 JUnit4 注释,但构建路径有点愚蠢。