@Parameters
测试的时候,测试的数据可以放在testng.xml文件中,被测试的方法通过@Parameters注解传递到测试方法中
parameterizedTest.java
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class parameterizedTest { @Test @Parameters("myName") public void testMethod(String myName) { System.out.println("myName:" + myName); } }
testng.xml:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" preserve-order="true"> <parameter name="myName" value="大盗如风"></parameter> <classes> <class name="parameterizedTest"></class> </classes> </test> </suite>
运行testng.xml,结果如下:
myName:大盗如风
@DataProvider
@Test(dataProvider = "")
如果测试的数据较多的情况下,很显然这种方式不适合,那么可以通过@DataProvider生成测试数据,通过@Test(dataProvider = "")使用测试数据
parameterizedTest2.java
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class parameterizedTest2 { // 定义数据源 @DataProvider(name = "test") public Object[][] createData() { return new Object[][]{{"dadaorufeng",29},{"zhangsan",28}}; } // 使用数据源 @Test(dataProvider = "test") public void verifyData(String name, int age) { System.out.println("name:" + name + ",age:" + age); } }
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" preserve-order="true"> <classes> <class name="parameterizedTest2"></class> </classes> </test> </suite>
运行testng.xml,结果如下:
name:dadaorufeng,age:29
name:zhangsan,age:28
刚才这段代码,定义数据源和使用数据源的代码在同一个class中,如果不在同一个class的话,怎么办呢?parameterizedTest2.java中定义数据源,parameterizedTest3.java使用数据源
parameterizedTest2.java
import org.testng.annotations.DataProvider; public class parameterizedTest2 { // 定义数据源 @DataProvider(name = "test") public static Object[][] createData() { return new Object[][]{{"dadaorufeng",29},{"zhangsan",28}}; } }
parameterizedTest3.java
import org.testng.annotations.Test; public class parameterizedTest3 { // 使用数据源 @Test(dataProvider = "test") public void verifyData(String name, int age) { System.out.println("name:" + name + ",age:" + age); } }
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" preserve-order="true"> <classes> <class name="parameterizedTest3"></class> </classes> </test> </suite>
直接运行testng.xml,结果如下,报错找不到test的@DataProvider :
org.testng.TestNGException: Method public void parameterizedTest3.verifyData(java.lang.String,int) requires a @DataProvider named : test at org.testng.internal.Parameters.findDataProvider(Parameters.java:268) at org.testng.internal.Parameters.handleParameters(Parameters.java:381) at org.testng.internal.Invoker.handleParameters(Invoker.java:1315) at org.testng.internal.Invoker.createParameters(Invoker.java:1022) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1122) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.runWorkers(TestRunner.java:1147) at org.testng.TestRunner.privateRun(TestRunner.java:749) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) at org.testng.SuiteRunner.run(SuiteRunner.java:223) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039) at org.testng.TestNG.runSuitesLocally(TestNG.java:964) at org.testng.TestNG.run(TestNG.java:900) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
修改parameterizedTest3.java,添加dataProviderClass = parameterizedTest2.class属性,指定数据源所在的类
import org.testng.annotations.Test; public class parameterizedTest3 { // 使用数据源 @Test(dataProvider = "test",dataProviderClass = parameterizedTest2.class) public void verifyData(String name, int age) { System.out.println("name:" + name + ",age:" + age); } }
再次运行testng.xml,结果如下,测试通过:
name:dadaorufeng,age:29
name:zhangsan,age:28