Andor参数化测试框架改造
上一篇 / 下一篇 2012-07-27 09:06:27 / 个人分类:杂谈
作为测试人员,您是否遇到过这样的测试场景?需要测试的方法都是大同小异:代码结构都是相同的,不同的仅仅是测试数据和期望值。为了解决这个问题,您可以采用juint参数化测试。51Testing软件测试网_6pLh Q8?
m%] \1Uc^{?8F0 如:需要测试的方法:
2@Ax\siKa#_(N0public int add(int a,int b){51Testing软件测试网#Mfcv(n8j6Q,UT f return a+b;51Testing软件测试网u_hEN%uR fZB }51Testing软件测试网7Xo8fd?kR |
+m&h EY`0 测试代码
*J,I!Pq`/as.BTL0@RunWith(Parameterized.class) //参数化测试的类必须有Parameterized测试运行器修饰 1g7\J?)Wh0|!H w.JtT0public class AddTest3 { 51Testing软件测试网#~5FP5Jv&M private int input1; 51Testing软件测试网(M'_ml6C;I*oh B } private int input2; &m vP J-R/e9a0private int expected;51Testing软件测试网0v(FC,^1\)W /**51Testing软件测试网M0y E,QFwuAH^ *准备数据。数据的准备需要在一个方法中进行,该方法需要满足一定的要求: b7f5caHY01)该方法必须由Parameters注解修饰 'w2M9q1z\4PI%ox02)该方法必须为public static的 8GB:eA$a#o0i s6Z8W!u03)该方法必须返回Collection类型 m/{*W!Wz,HTr)wgF~y04)该方法的名字不做要求51Testing软件测试网 y_r'JA _6L8u O 5)该方法没有参数51Testing软件测试网%Jf.f9I kh4Oi6^ * @return ke!QF@)n&`0*/ 3F2d'w1k |/V%Y m}-v%K0@Parameters 51Testing软件测试网e7B6BP+w]9O.q @SuppressWarnings("unchecked") "To-o4u4e*WkJ0public static Collection prepareData(){ A hojbSH0Object [][] bject = {{-1,-2,-3},{0,2,2},{-1,1,0},{1,2,3}}; 51Testing软件测试网8Bs#wG7Mw"l L[ return Arrays.asList(object); 5lViG L[\4V2G0} 51Testing软件测试网1]aRrex$GC&pf 51Testing软件测试网7lo{n%S)dTWd public AddTest3(int input1,int input2,int expected){ &^{#q:k [,iS0this.input1 = input1; OD D+piW$[h*X)X0this.input2 = input2; 0F sS/A M0this.expected = expected; 51Testing软件测试网-?a#}1D`Q%Jr~ } 51Testing软件测试网"J4af'Hwd ,~ Q-Zk"dN[2i![W0 @Test 6Z2C6OOM0public void testAdd(){ )hN8q&j |