DataProvider
@DataProvider 注解帮助我们编写数据驱动的测试用例。@DataProvider 注解使我们能够通过传递不同的数据集多次运行测试方法。
以下是@DataProvider 注解支持的属性列表:
属性描述name此数据提供者的名称。如果未提供,则此数据提供程序的名称将自动设置为方法的名称。带注解的方法必须返回一个 Object[][],其中每个 Object[] 都可以分配测试方法的参数列表。parallel如果设置为true,则使用此数据提供程序生成的测试将并行运行。默认值为false。
dataProvider作为数据提供者只能返回Object[][]和Iterator<Object[]>类型的数据。
Iterator<Object[]>类型
packagecom.testng.example.testngdemoone;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclassTestngDemoOneApplicationTest{@DataProvider(name="dataProvider-testcase")publicIterator<Object[]>dataProvider(){List<Object> testCasePaths =newArrayList<Object>();
testCasePaths.add("test1");
testCasePaths.add("test2");
testCasePaths.add("test3");
testCasePaths.add("test4");List<Object[]> path =newArrayList<>();for(Object obj:testCasePaths){
path.add(newObject[]{obj});}return path.iterator();}@Test(dataProvider ="dataProvider-testcase")publicvoidtestDataProviderIterator(String path){System.out.println(path);}}
测试执行结果
test1
test2
test3
test4===============================================
Default Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
Object[][]类型
packagecom.testng.example.testngdemoone;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclassTestngDemoOneApplicationTest{@TestvoidcontextLoads(){System.out.println("this is the first surccess test");}@TestvoiderrorTests(){System.out.println("this is the first error test");assertfalse;}@DataProvider(name="dataProvider-testcase")publicIterator<Object[]>dataProvider(){List<Object> testCasePaths =newArrayList<Object>();
testCasePaths.add("test1");
testCasePaths.add("test2");
testCasePaths.add("test3");
testCasePaths.add("test4");List<Object[]> path =newArrayList<>();for(Object obj:testCasePaths){
path.add(newObject[]{obj});}return path.iterator();}@Test(dataProvider ="dataProvider-testcase")publicvoidtestDataProviderIterator(String path){System.out.println(path);}@Test(dataProvider ="data")publicvoiddataProviderTest(String name,int age,String hobby){System.out.println(name +" "+ age +" "+ hobby);}@DataProvider(name ="data")Object[][]getData(){returnnewObject[][]{{"June",22,"Sing"},{"Jack",22,"Football"},{"Duke",22,"Chess"}};}}packagecom.testng.example.testngdemoone;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Test;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;publicclassTestngDemoOneApplicationTest{@Test(dataProvider ="data")publicvoiddataProviderTest(String name,int age,String hobby){System.out.println(name +" "+ age +" "+ hobby);}@DataProvider(name ="data")Object[][]getData(){returnnewObject[][]{{"June",22,"Sing"},{"Jack",22,"Football"},{"Duke",22,"Chess"}};}}
June 22 Sing
Jack 22 Football
Duke 22 Chess===============================================
Default SuiteTotal tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
版权归原作者 糖果店的幽灵 所有, 如有侵权,请联系我们删除。