Part 1: 背景
自动化在部门内部推行了大半年时间,由于电话号码,邮件账户等信息希望能够实现从外部读取,并使用,避免使用同一组数据,也为了让脚本更加利于维护,因此部门内部提出了把测试数据分离的观点。
Part 2: 思路
刚开始的时候,想的是把文本里的文件用IO流读进来,然后在进行分配,但问题就来了,读取数据简单,但怎么分配成了一个难题。后来通过查资料得知,Junti4已经实现了该功能,使用Parameters即可。
核心步骤如下:
- 更改运行器为Parameterized.class
- 定义数据源,使用@Parameters进行标记,注意返回类型为Collection或者是数组
- 创建一个带参数的构造方法,用来进行参数的分配。
Part 3 代码实现
标记运行器
定义用来运行的运行器
1 2 3 4 5
| @RunWith(Parameterized.class) public class Pa { .... }
|
定义数据池
Junit会读取数组里的内容,如果是单个数据可以定义为一个数组,如果需要从外部读取,则把读取的数据按照格式进行保存即可
1 2 3 4 5 6 7
| @Parameterized.Parameters public static Collection<Object[]> appname() { return Arrays.asList(new Object[][]{ {"电话", "com.android.dialer"}, {"电子邮件", "com.android.email"}, {"短信", "com.android.dialer"} }); }
|
定义构造方法
主要是用来制定读取到的参数怎么在程序内部使用,需要几个参数则传递几个参数
1 2 3 4 5 6 7 8 9 10 11
| //定义传参名字 private String target; private String result; // 用到几个参数,则这里需要定义几个参数哦 public Pa(String a, String b) { target = a; result = b; }
|
或者使用@Parameter来替代构造参数。
1 2 3 4 5
| //如果使用parameter来进行标注,那么必须指定为Public @Parameter//如果不加则默认为第一个元素即,下标为0 public String target; @Parameter(1) public String result;
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Test public void openapp() throws InterruptedException, UiObjectNotFoundException { UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager(); mDevice.findObject(new UiSelector().description("应用")).click(); Thread.sleep(1000); mDevice.findObject(new UiSelector().text(target)).click(); Thread.sleep(1000); String curentPackageName = mDevice.getCurrentPackageName(); Thread.sleep(1000); System.out.println("ABS" + curentPackageName); Assert.assertEquals("Test Fail,The Package info not match!", result, curentPackageName); mDevice.pressBack(); mDevice.pressBack(); mDevice.pressBack(); mDevice.pressBack(); }
|
Part 4 完整例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| @RunWith(Parameterized.class) public class Pa { //定义数据源 @Parameterized.Parameters public static Collection<Object[]> appname() { //根据需要定义参数的个数 return Arrays.asList(new Object[][]{ {"电话", "com.android.dialer",}, {"电子邮件", "com.android.email"}, {"短信", "com.android.dialer"} }); } //定义传参名字 private String target; private String result; //定义参数的传递方式,这里是用到了两个参数 public Pa(String a, String b) { target = a; result = b; } //运行的例子 @Test public void openapp() throws InterruptedException, UiObjectNotFoundException { UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); PackageManager packageManager = InstrumentationRegistry.getTargetContext().getPackageManager(); mDevice.findObject(new UiSelector().description("应用")).click(); Thread.sleep(1000); mDevice.findObject(new UiSelector().text(target)).click(); Thread.sleep(1000); String curentPackageName = mDevice.getCurrentPackageName(); Thread.sleep(1000); System.out.println("ABS" + curentPackageName); Assert.assertEquals("Test Fail,The Package info not match!", result, curentPackageName); mDevice.pressBack(); mDevice.pressBack(); mDevice.pressBack(); mDevice.pressBack(); } }
|