testng在类 里面的数据驱动
package driver_test; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestngDataProvider { /*@DataProvider(name="DP1") public Object[][] createData() { Object[][] createdata={ { "001", "Jack", "London" }, { "002", "John", "New York" }, { "003", "Mary", "Miami" }, { "004", "George", "california"} }; return (createdata); }*/ @DataProvider(name="DP1") public Object[][] createData(){ return new Object[][] { { "001", "Jack", "London" }, { "002", "John", "New York" }, { "003", "Mary", "Miami" }, { "004", "George", "california"} }; } @Test(dataProvider="DP1") public void testcase(String empid, String empName, String city){ System.out.println("empid"); System.out.println("empName"); System.out.println("city"); } }
右键------->RunAs-------->testng
运行结果如下:
empid empName city empid empName city empid empName city empid empName city PASSED: testcase("001", "Jack", "London") PASSED: testcase("002", "John", "New York") PASSED: testcase("003", "Mary", "Miami") PASSED: testcase("004", "George", "california") =============================================== Default test Tests run: 4, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 4, Failures: 0, Skips: 0 ===============================================
另外一种,在dataprovider里面写入方法
package driver_test; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class driver_data { @DataProvider(name = "iterator") public Iterator<Object[]> getData() { Set<Object[]> set = new HashSet<Object[]>(); set.add(new String[] { "hello" }); Iterator<Object[]> iterator = set.iterator(); return iterator; } @Test(dataProvider = "iterator") public void testIteraorData(String iterator) { System.err.println("iterator .. " + iterator); } }
右键------->RunAs-------->testng
运行结果如下:
iterator .. hello PASSED: testIteraorData("hello") =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================