前面寫過一個(gè)無參的數(shù)據(jù)提供者方法,,還有三種有參數(shù)據(jù)提供者方法。 一,、Method表示當(dāng)前測(cè)試的方法 Dataprovidertest.java代碼
注意這里我的Dataprovider的name值沒有指定,,默認(rèn)用的是方法名。 Testng.xml代碼 <?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="DataProviderTest"> <classes> <class name="com.tzf.testng.DataProviderTest" /> </classes> </test> </suite> 運(yùn)行結(jié)果代碼 RemoteTestNG starting method1 received:1 method2 received:2 method3 received:3 RemoteTestNG finishing: 145 ms 二,、ITestContent當(dāng)前測(cè)試的上下文信息 Dataprovidertest2.java代碼 package com.tzf.testng; import java.util.Random; import org.testng.ITestContext; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderTest2 { @DataProvider public Object[][] providerMethod(ITestContext context){ String[] groups = context.getIncludedGroups(); int size = 2; for(String group : groups){ if(group.equals("function-test")){ size = 10 ; break; } } Object[][] result = new Object[size][]; Random r = new Random(); for(int i=0;i<size;i++){ result[i] = new Object[]{new Integer(r.nextInt())}; } return result; } @Test(dataProvider="providerMethod",groups={"function-test","unit-test"}) public void testmethod1(Integer n){ System.out.println("method1 received:"+n); } } Testng.xml代碼 <?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="DataProviderTest2"> <groups> <run> <include name="function-test"/> <exclude name="unit-test"/> </run> </groups> <classes> <class name="com.tzf.testng.DataProviderTest2" group="function-test"/> </classes> </test> </suite> 運(yùn)行結(jié)果代碼 RemoteTestNG starting method1 received:-1987654923 method1 received:338477914 method1 received:1910366338 method1 received:107205369 method1 received:-1018164959 method1 received:-2060129321 method1 received:-803464435 method1 received:-814156781 method1 received:-1075504680 method1 received:-774924512 RemoteTestNG finishing: 151 ms |
|