总结:
0.@Dataprovider 所修饰的方法必须 return Object[][] ; @Facotry 所修饰的方法必须return Object[] ;
1.在测试场景中经常会遇到一个人执行多个case,那么多个人没人都执行一遍这个case的时候就要用到@Factory创建多个对象了,类似于QTP中的迭代;
2.如果测试相关联的类中只有一个带参的构造方法,那么在执行测试的时候可能会抛一个无法初始化某个类的错误,这个时候就要使用@Factory来进行初始化了;
说明:一个人执行多次case和多个人每人都执行多次case是不一样的测试场景,类似于模拟多用户的场景,比如A用户有执行lognin()的权限,而B用户没有这个权限,虽然A和B都是一个用户对象,但他们执行的结果却不一样 ;
以上仅代表个人理解哈,如有不同,+316567803 或者留言进行讨论 ;
1.@Datatprovider 可以放在一个类中为@Test 和 @Factory 提供参数初始化 比如:
1 package main.test; 2 3 import org.testng.annotations.DataProvider; 4 import org.testng.annotations.Factory; 5 import org.testng.annotations.Test; 6 public class BB { 7 private int id ; 8 private String name ; 9 private int age ; 10 BB(){} 11 public BB(int id, String name, int age) { 12 this.id = id; 13 this.name = name; 14 this.age = age; 15 } 16 public String toString() { 17 return "BB{" + 18 "id=" + id + 19 ", name=‘" + name + ‘\‘‘ + 20 ", age=" + age + 21 ‘}‘; 22 } 23 24 25 /* 26 *1.为了演示这里提供了2个@DataProvider(dataPraovide1 和 dataPraovide2) ; 27 * >dataPraovide1 作为Test数据的数据源 ; 28 * >dataPraovide2 作为@Factory的数据化数据源; 29 *2.@Factory为BB类提供初始化 ; 30 *3.@Test 为BB类的print(); 31 */ 32 33 @DataProvider(name = "db1") 34 public Object[][] dataPraovide_1() { 35 return new Object[][]{ 36 {1, "jack1", 21}, {4, "jack4", 24}, {7, "jack7", 27}, 37 {2, "jack2", 22}, {5, "jack5", 25}, {8, "jack8", 28}, 38 {3, "jack3", 23}, {6, "jack6", 26}, {9, "jack9", 29} 39 }; 40 } 41 @DataProvider(name = "db") 42 public Object[][] dataPraovide_0() { 43 return new Object[][]{ 44 {1, "jack1", 21}, {4, "jack4", 24}, {7, "jack7", 27}, 45 {2, "jack2", 22}, {5, "jack5", 25}, {8, "jack8", 28}, 46 {3, "jack3", 23}, {6, "jack6", 26}, {9, "jack9", 29} 47 }; 48 } 49 //使用db数据源没次创建1个BB的对象,然后使用这个对象执行db1的测试案例(9×9=81次) ; 50 @Factory(dataProvider = "db") 51 public Object[] createObj_1(int id, String name, int age) { 52 BB[] bbArray ={new BB(id, name, age)}; 53 return bbArray; 54 } 66 @Test( dataProvider = "db1") 67 public void print(int id ,String name,int age) { 68 System.out.println(this.toString()); 69 System.out.println("-------"+id+"-----------"+name+"------------"+age); 70 } }
执行结果:
2.将@Factory 和 @DataPrivade 放在2个类中进行处理 ;
GG.class 为BB.class提示数据支持;
1 package main.test; 2 3 import org.testng.annotations.DataProvider; 4 import org.testng.annotations.Factory; 5 6 public class GG { 7 /* 8 *1.为了演示这里提供了2个@DataProvider(dataPraovide1 和 dataPraovide2) ; 9 * >dataPraovide1 作为Test数据的数据源 ; 10 * >dataPraovide2 作为@Factory的数据化数据源; 11 *2.@Factory为BB类提供初始化 ; 12 *3.@Test 为BB类的print(); 13 */ 14 @DataProvider(name = "db1") 15 public static Object[][] dataPraovide1() { 16 return new Object[][]{ 17 {1, "jack1", 21}, {4, "jack4", 24}, {7, "jack7", 27}, 18 {2, "jack2", 22}, {5, "jack5", 25}, {8, "jack8", 28}, 19 {3, "jack3", 23}, {6, "jack6", 26}, {9, "jack9", 29} 20 }; 21 } 22 @DataProvider(name = "db") 23 public Object[][] dataPraovide() { 24 return new Object[][]{ 25 {1, "jack1", 21}, {4, "jack4", 24}, {7, "jack7", 27}, 26 {2, "jack2", 22}, {5, "jack5", 25}, {8, "jack8", 28}, 27 {3, "jack3", 23}, {6, "jack6", 26}, {9, "jack9", 29} 28 }; 29 } 30 //使用db数据源没次创建1个BB的对象,然后使用这个对象执行db1的测试案例(9×9=81次) ; 31 /* @Factory(dataProvider = "db") 32 public Object[] createObj_1(int id, String name, int age) { 33 BB[] bbArray ={new BB(id, name, age)}; 34 return bbArray; 35 }*/ 36 //使用db数据源没次创建10个BB的对象,然后使用这个对象执行db1的测试案例(10x9×9=810次) ; 37 @Factory(dataProvider = "db") 38 public Object[] createObj(int id, String name, int age) { 39 BB[] bbArray = new BB[10]; 40 for (int i = 0; i < bbArray.length; i++) { 41 bbArray[i] = new BB(id, name, age); 42 } 43 44 return bbArray; 45 } 46 }
BB.class
1 package main.test; 2 3 import org.testng.annotations.Test; 4 public class BB { 5 private int id ; 6 private String name ; 7 private int age ; 8 BB(){} 9 public BB(int id, String name, int age) { 10 this.id = id; 11 this.name = name; 12 this.age = age; 13 } 14 public String toString() { 15 return "BB{" + 16 "id=" + id + 17 ", name=‘" + name + ‘\‘‘ + 18 ", age=" + age + 19 ‘}‘; 20 } 21 22 //如果不放在一个类中这里要制定dataProviderClass 类;并且dataProvider = "db1" 的方法要为静态的,如果不为静态的会跑错; 23 @Test( dataProvider = "db1", dataProviderClass = main.test.GG.class ) 24 public void print1(int id ,String name,int age) { 25 System.out.println(this.toString()); 26 System.out.println("-------"+id+"-----------"+name+"------------"+age); 27 } 28 }
tesng.xlm
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="All Test Suite"> <test verbose="2" preserve-order="true" name="TESTPROVIDER"> <classes> <!-- 这里必须加载GG类不然执行抛错 --> <class name="main.test.GG"/> <class name="main.test.BB"> <methods> <include name="print1"/> </methods> </class> </classes> </test> </suite>
execute.bat 文件
@echo off set xmlPath=C:\CustomSofter\develop\IDEA\IDEAWorkSpace\C3p0TestPrictice set lib=C:\CustomSofter\develop\IDEA\IDEAWorkSpace\C3p0TestPrictice\out\artifacts\C3p0TestPrictice_jar set classpath=%lib%\C3p0TestPrictice.jar;%lib%\jcommander-1.30.jar;%lib%\testng-6.8.8.jar; java org.testng.TestNG %xmlPath%\testng.xml pause
下面这个图主要是和execute.bat 文件相对照的
3.执行结果:
4.如果db1方法不为静态的错误: