扩展InstrumentationTestRunner,传递自定义参数
发表于:2024-11-27 作者:热门IT资讯网编辑
编辑最后更新 2024年11月27日,通过继承InstrumentationTestRunner,就可以传递自定义参数到TestRunner。这里是传递port、packageName、activityName,其中port是用于在手机侧
通过继承InstrumentationTestRunner,就可以传递自定义参数到TestRunner。这里是传递port、packageName、activityName,其中port是用于在手机侧启动一个TCP服务的端口,packageName和activityName是用于指定首个启动的activity的参数配置:
- import android.os.Bundle;
- import android.test.InstrumentationTestRunner;
- public class AthrunInstrumentationTestRunner extends InstrumentationTestRunner {
- private static String packageName = "com.taobao.fario";
- private static String activityName = "com.taobao.fario.MainActivity";
- private static String port = "1234";
- public static String getPort() {
- return port;
- }
- public static String getPackageName() {
- return packageName;
- }
- public static String getActivityName() {
- return activityName;
- }
- @Override
- public void onCreate(Bundle arguments) {
- if (arguments != null) {
- String port = arguments.getString("port");
- String packageName = arguments.getString("packageName");
- String activityName = arguments.getString("activityName");
- if (port != null) {
- AthrunInstrumentationTestRunner.port = port;
- }
- if (packageName != null) {
- AthrunInstrumentationTestRunner.packageName = packageName;
- }
- if (activityName != null) {
- AthrunInstrumentationTestRunner.activityName = activityName;
- }
- }
- super.onCreate(arguments);
- }
执行命令变成:
- adb -s SH14MTJ01900 shell am instrument -w -e port 1234 -e class org.athrun.remoterunner.TravelTest#test org.athrun.remoterunner/org.athrun.instrumentation.AthrunInstrumentationTestRunner
在Testcase中如何使用传入的packageName和activityName:
- public class TravelTest extends ActivityInstrumentationTestCase2 {
- private static String getPkgName() {
- String packageName = AthrunInstrumentationTestRunner.getPackageName();
- return packageName;
- }
- private static String getActivityClassStr() throws ClassNotFoundException {
- String activityName = AthrunInstrumentationTestRunner.getActivityName();
- return activityName;
- }
- public TravelTest() throws Exception {
- super(getPkgName(), getActivityClassStr());
- }
-e 是属于扩展参数,使用这种方法是可以不断扩展的。