@DataProvider ITestContext 参数

 1 package roger.testng;
 2 
 3 import java.util.Random;
 4 
 5 import org.testng.ITestContext;
 6 import org.testng.annotations.DataProvider;
 7 import org.testng.annotations.Test;
 8 
 9 /*
10  *  数据提供者在方法签名中声明了一个 ITestContext 类型的参数
11  *  testng 会将当前的测试上下文设置给它
12  *  
13  */
14 public class TestDataProviderITestContext {
15     @DataProvider
16     public Object[][] randomIntegers(ITestContext context) {
17         String[] groups= context.getIncludedGroups();
18         int size = 2;
19         for (String group : groups) {
20             System.out.println("--------------" + group);
21             if (group.equals("function-test")) {
22                 size = 10;
23                 break;
24             }
25         }
26         
27         Object[][] result = new Object[size][];
28         Random r = new Random();
29         for (int i = 0; i < size; i++) {
30             result[i] = new Object[] {new Integer(r.nextInt())};
31         }
32         
33         return result;
34     }
35     
36     // 如果在 unite-test 组中执行, 将返回2个随机整数构成数组;
37     // 如果在 function-test 组中执行, 将返回 10 个随机整数构成数组
38     @Test(dataProvider = "randomIntegers", groups = {"unit-test", "function-test"})
39     public void random(Integer n) {
40         System.out.println(n);
41     }
42     
43 }

通过 testng.xml 指定运行 unite-test 组还是 function-test 组。

posted @ 2014-07-17 15:20  Roger|  阅读(3298)  评论(1编辑  收藏  举报