下面是@DataProvider有name和没有name时
有name的时候可以引用name 即:@DataProvider(name="testData")---------->@Test(dataProvider="testData")
package data_driver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestDataDriven { @DataProvider(name="testData") public Object[][] dataProvider(){ return new Object[][]{{1,2},{2,3},{3,4}}; } @Test(dataProvider="testData") public void testDataDriven(int a,int b){ System.out.println("this is :"+(a+b)); } }
右键----->RunAs----->TestNG test 运行结果如下:
[TestNG] Running: C:UserschenjiaAppDataLocalTemp estng-eclipse--2116911479 estng-customsuite.xml this is :3 this is :5 this is :7 PASSED: testDataDriven(1, 2) PASSED: testDataDriven(2, 3) PASSED: testDataDriven(3, 4) =============================================== Default test Tests run: 3, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
没有name的时候可以引用方法名即:
package data_driver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestDataDriven{ @DataProvider public Object[][] dataprovider(){ return new Object[][]{{1,2},{2,3},{3,4},{4,5},{5,6}}; } @Test(dataProvider="dataprovider") public void testDataDriven(int a, int b){ System.out.println("this is :"+ (a + b)); } }
右键----->RunAs----->TestNG test 运行结果如下:
[TestNG] Running: C:UserschenjiaAppDataLocalTemp estng-eclipse-1345547329 estng-customsuite.xml this is :3 this is :5 this is :7 this is :9 this is :11 PASSED: testDataDriven(1, 2) PASSED: testDataDriven(2, 3) PASSED: testDataDriven(3, 4) PASSED: testDataDriven(4, 5) PASSED: testDataDriven(5, 6) =============================================== Default test Tests run: 5, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 5, Failures: 0, Skips: 0 ===============================================