您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > TestNG
TestNG之参数化测试
作者:网络转载 发布时间:[ 2015/7/28 14:45:37 ] 推荐标签:软件测试工具 单元测试

  运行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

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd