热门IT资讯网

扩展InstrumentationTestRunner,传递自定义参数

发表于:2024-11-27 作者:热门IT资讯网编辑
编辑最后更新 2024年11月27日,通过继承InstrumentationTestRunner,就可以传递自定义参数到TestRunner。这里是传递port、packageName、activityName,其中port是用于在手机侧

通过继承InstrumentationTestRunner,就可以传递自定义参数到TestRunner。这里是传递port、packageName、activityName,其中port是用于在手机侧启动一个TCP服务的端口,packageName和activityName是用于指定首个启动的activity的参数配置:

  1. import android.os.Bundle;
  2. import android.test.InstrumentationTestRunner;
  3. public class AthrunInstrumentationTestRunner extends InstrumentationTestRunner {
  4. private static String packageName = "com.taobao.fario";
  5. private static String activityName = "com.taobao.fario.MainActivity";
  6. private static String port = "1234";
  7. public static String getPort() {
  8. return port;
  9. }
  10. public static String getPackageName() {
  11. return packageName;
  12. }
  13. public static String getActivityName() {
  14. return activityName;
  15. }
  16. @Override
  17. public void onCreate(Bundle arguments) {
  18. if (arguments != null) {
  19. String port = arguments.getString("port");
  20. String packageName = arguments.getString("packageName");
  21. String activityName = arguments.getString("activityName");
  22. if (port != null) {
  23. AthrunInstrumentationTestRunner.port = port;
  24. }
  25. if (packageName != null) {
  26. AthrunInstrumentationTestRunner.packageName = packageName;
  27. }
  28. if (activityName != null) {
  29. AthrunInstrumentationTestRunner.activityName = activityName;
  30. }
  31. }
  32. super.onCreate(arguments);
  33. }

执行命令变成:

  1. 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:

  1. public class TravelTest extends ActivityInstrumentationTestCase2 {
  2. private static String getPkgName() {
  3. String packageName = AthrunInstrumentationTestRunner.getPackageName();
  4. return packageName;
  5. }
  6. private static String getActivityClassStr() throws ClassNotFoundException {
  7. String activityName = AthrunInstrumentationTestRunner.getActivityName();
  8. return activityName;
  9. }
  10. public TravelTest() throws Exception {
  11. super(getPkgName(), getActivityClassStr());
  12. }

-e 是属于扩展参数,使用这种方法是可以不断扩展的。

0