package roger.testng; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; public class TestDataProviderParameter {
@DataProvider(name = "test1")
public Object[][] testData() {
return new Object[][] {
{"roger1", 26},
{"roger2", 27},
{"roger3", 28},
};
} @Test(dataProvider = "test1")
public void testParameter(String name, int age) {
System.out.println("name: " + name + ", age: " + age);
}
}
执行结果:
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--794089435\testng-customsuite.xml name: roger1, age: 26
name: roger2, age: 27
name: roger3, age: 28
PASSED: testParameter("roger1", 26)
PASSED: testParameter("roger2", 27)
PASSED: testParameter("roger3", 28) ===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 1 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@9cdc393: 45 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6cf1156e: 5 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@4ae523ed: 46 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@6d45ca41: 7 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@923ef32: 25 ms
@DataProvider 的 name 属性是可选的,如果不指定它,就会默认使用方法的名称。
如上代码中不指定 name 的名称,则默认是 @DataProvider(name = "testData")