数据与test脚本不在同一文件时,下面是数据:
package data_driver_two_parts; import org.testng.annotations.DataProvider; public class TestDataProvider { @DataProvider public static Object[][] DataMethodNoName(){ return new Object[][]{ {"data with no name 1"}, {"data with no name 2"}, {"data with no name 3"} }; } @DataProvider(name="datamethodwithname") public static Object[][] DataMethodwithName(){ return new Object[][]{ {"data with name 4"}, {"data with name 5"}, {"data with name 6"} }; } }
下面是test脚本:
package data_driver_two_parts; import org.testng.annotations.Test; public class testcase_for_driver { @Test(dataProvider="DataMethodNoName",dataProviderClass=data_driver_two_parts.TestDataProvider.class) public void testDataDriven_1(String a){ System.out.println("this is : "+a); } @Test(dataProvider="datamethodwithname",dataProviderClass=data_driver_two_parts.TestDataProvider.class) public void testDataDriven_2(String b){ System.out.println("l am "+b); } }
右键----->RunAs----->TestNG test 运行结果如下:
[TestNG] Running: C:UserschenjiaAppDataLocalTemp estng-eclipse--112979388 estng-customsuite.xml this is : data with no name 1 this is : data with no name 2 this is : data with no name 3 l am data with name 4 l am data with name 5 l am data with name 6 PASSED: testDataDriven_1("data with no name 1") PASSED: testDataDriven_1("data with no name 2") PASSED: testDataDriven_1("data with no name 3") PASSED: testDataDriven_2("data with name 4") PASSED: testDataDriven_2("data with name 5") PASSED: testDataDriven_2("data with name 6") =============================================== Default test Tests run: 6, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 6, Failures: 0, Skips: 0 ===============================================