@Factory和@DataProvider的区别
2015-05-31 14:48
211 查看
DataProvider: A test method that uses DataProvider will be executed a multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.
Factory: A factory will execute all the test methods present inside a test class using a separate instance of the respective class
也就是说,在DataProvider中测试方法将使用相同的实例执行测试类中的测试方法。
在Factory中将执行所有测试方法,使用一个单独的各自的类的实例,将动态的创建类的实例,你可以多次运行测试类。
举个例子比较好区分:对于登录到一个网站的测试,如果你想运行这个测试类多次,登入网站,那么可以使用Factory来动态创建测试类来测试。如果你想要根据不同的用户名和密码来登录网站那么就可以使用DataProvider,每一次的登录都使用不同的登录数据。
使用DataProvider:
结果:
说明:不同的数据,但是使用的测试类是相同的,beforeclass只被执行了一次,说明只创建了一个测试类实例。
使用Factory:
结果:
说明:beforeClass在每一个测试方法之前都执行了,为每一个测试都生成了一个测试的实例。
这个就是他们之间的区别。
Factory: A factory will execute all the test methods present inside a test class using a separate instance of the respective class
也就是说,在DataProvider中测试方法将使用相同的实例执行测试类中的测试方法。
在Factory中将执行所有测试方法,使用一个单独的各自的类的实例,将动态的创建类的实例,你可以多次运行测试类。
举个例子比较好区分:对于登录到一个网站的测试,如果你想运行这个测试类多次,登入网站,那么可以使用Factory来动态创建测试类来测试。如果你想要根据不同的用户名和密码来登录网站那么就可以使用DataProvider,每一次的登录都使用不同的登录数据。
使用DataProvider:
public class DataProviderClass { @BeforeClass public void beforeClass() { System.out.println("Before class executed"); } @Test(dataProvider = "dataMethod") public void testMethod(String param) { System.out.println("The parameter value is: " + param); } @DataProvider public Object[][] dataMethod() { return new Object[][] { { "one" }, { "two" } }; } }
结果:
Before class executed The parameter value is: one The parameter value is: two PASSED: testMethod("one") PASSED: testMethod("two")
说明:不同的数据,但是使用的测试类是相同的,beforeclass只被执行了一次,说明只创建了一个测试类实例。
使用Factory:
public class SimpleTest { private String param = ""; public SimpleTest(String param) { this.param = param; } @BeforeClass public void beforeClass() { System.out.println("Before SimpleTest class executed."); } @Test public void testMethod() { System.out.println("testMethod parameter value is: " + param); } } public class SimpleTestFactory { @Factory public Object[] factoryMethod() { return new Object[] { new SimpleTest("one"), new SimpleTest("two") }; } }
结果:
Before SimpleTest class executed. testMethod parameter value is: two Before SimpleTest class executed. testMethod parameter value is: one PASSED: testMethod PASSED: testMethod
说明:beforeClass在每一个测试方法之前都执行了,为每一个测试都生成了一个测试的实例。
这个就是他们之间的区别。
相关文章推荐
- windows 下 jdk tomcat7配置
- "晴天"天气app的开源库逐步学习(二)
- C++的内存模型和名称空间
- java文件读取全了解
- The Data Revolution Speaker(Hadoop之父Doug Cutting在清华的讲座)
- IT痴汉的工作现状34-技术选型
- MAC OS 常用软件及开发工具
- Android网络框架Volley(体验篇)
- 图形界面
- Internal Sorting: Straight Two-way Merge Sort: Sorting by Merging
- Android多线程任务的优化1:AsyncTask的缺陷
- 十二周——多态性——阅读项目(1)
- VMware vSphere 对 Microsoft 群集解决方案的支持详细列表
- 第十二周项目4-2:点,圆的关系-面积比较
- ubuntu 14.04 LTS eclipse tomcat配置
- Leetcode_189 Rotate Array
- ListView异步加载网络图片完美版之双缓存技术
- Red Hat Enterprise 6.5配置优化系统
- 如何在CSDN博客中显示图片?
- linux scp