testng的@Factory及其与@DataProvider的区别
Factory,顾名思意就是工厂,也就是工厂方法,在软件开发中一般结合多态使用,用来根据不同的条件创建不同的类对象。
在这里,Factory一般用来创建一个测试类的多个实例,每个实例属性不同,以执行不同的测试,Factory构造实例的方法必须返回Object[],也就是一组测试类的实例。
以testng官网的例子来说明,测试类如下,在测试用例testServer中,访问m_numberOfTimes次web页面,这里打印出了访问的次数和执行该用例的实例地址。
public class WebTest {
private int m_numberOfTimes;
public WebTest(int numberOfTimes) {
m_numberOfTimes = numberOfTimes;
} @Test
public void testServer() {
for (int i = 0; i < m_numberOfTimes; i++) {
// access the web page
System.out.println("Access the web page, times " + i + ", the instance is " + this);
}
}
}
构造测试类WebTest实例的工厂如下,该工程创建了5个WebTest实例:
public class WebTestFactory {
@Factory
public Object[] createInstances() {
Object[] result = new Object[5];
for (int i = 0; i < 5; i++) {
result[i] = new WebTest(i+1);
}
return result;
}
}
然后在XML文件中指定测试类为WebTestFactory,即可运行测试,注意:只需指定工厂类即可,无需再指定测试类。
<suite name="suite1"> <test name="test1" verbose="2">
<classes>
<class name="sea.WebTestFactory" />
</classes>
</test> </suite>
运行结果如下,可见总共执行了5个用例,每个用例访问web页面的次数不同。
Access the web page, times 0, the instance is sea.WebTest@1593948d
Access the web page, times 1, the instance is sea.WebTest@1593948d
Access the web page, times 2, the instance is sea.WebTest@1593948d Access the web page, times 0, the instance is sea.WebTest@1b604f19
Access the web page, times 1, the instance is sea.WebTest@1b604f19
Access the web page, times 2, the instance is sea.WebTest@1b604f19
Access the web page, times 3, the instance is sea.WebTest@1b604f19 Access the web page, times 0, the instance is sea.WebTest@482f8f11
Access the web page, times 1, the instance is sea.WebTest@482f8f11 Access the web page, times 0, the instance is sea.WebTest@51565ec2 Access the web page, times 0, the instance is sea.WebTest@7823a2f9
Access the web page, times 1, the instance is sea.WebTest@7823a2f9
Access the web page, times 2, the instance is sea.WebTest@7823a2f9
Access the web page, times 3, the instance is sea.WebTest@7823a2f9
Access the web page, times 4, the instance is sea.WebTest@7823a2f9 PASSED: testServer
PASSED: testServer
PASSED: testServer
PASSED: testServer
PASSED: testServer
可以看到Factory的使用也比较简单,思考一下,在上面的例子中一个用例运行了5次,每次访问页面的次数不同,那么用DataProvider能否实现呢?
采用DataProvider实现的代码如下,这里通过DataProvider来提供每个用例访问web页面的次数,一共5个参数,用例会执行5次:
public class Test1 { @DataProvider(name = "data1")
public Object[][] createdata() {
return new Object[][] {
{1},
{2},
{3},
{4},
{5}
};
} @Test(dataProvider = "data1")
public void testServer(int m_numberOfTimes) {
for (int i = 0; i < m_numberOfTimes; i++) {
// access the web page
System.out.println("Access the web page, times " + i + ", the instance is " + this);
}
} }
执行结果如下:
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 3, the instance is sea.Test1@58651fd0
Access the web page, times 0, the instance is sea.Test1@58651fd0
Access the web page, times 1, the instance is sea.Test1@58651fd0
Access the web page, times 2, the instance is sea.Test1@58651fd0
Access the web page, times 3, the instance is sea.Test1@58651fd0
Access the web page, times 4, the instance is sea.Test1@58651fd0
PASSED: testServer(1)
PASSED: testServer(2)
PASSED: testServer(3)
PASSED: testServer(4)
PASSED: testServer(5)
可以看到,就访问web页面的次数来说,这里结果与Factory方法没有什么不同,那这两者有什么区别呢?
DataProvider:为测试用例提供参数,有多少组参数就会执行多少次用例,因此它是让一个测试类实例的某个方法执行多次,但每次执行都是采用的同一个实例(从上面结果的实例地址可以看出)。
Factory:创建一个测试类的多个实例,每个实例中的所有测试用例都会被执行,因此它是让一个测试类被执行多次,每次执行采用的是不同实例。
因此,如果想要使用不同的测试数据执行一个测试用例多次,那么采用DataProvider;如果想要多次执行一个测试类的所有用例,那么采用Factory。
当然,Factory与DataProvider也可结合在一起使用,请看下面例子:
在测试类Test1中有2个用例,其中用例test2通过dataProvider提供参数,而在DataProvider中提供了3组参数,因此测试类Test1每次会执行4个用例;
在工厂类FactoryWithDataprovider中,通过DataProvider为工厂方法提供参数,因此工厂方法会创建2个Test1的实例。
public class Test1 {
private String name;
public Test1(String name) {
this.name = name;
} @Test
public void test1() {
System.out.println("test1: His name is " + name);
} @Test(dataProvider = "data1")
public void test2(String hobby) {
System.out.println("test2: " + name + " likes " + hobby);
} @DataProvider(name = "data1")
public Object[][] createdata() {
return new Object[][] {
{"baozoumanhua"},
{"movie"},
{"music"}
};
}
} public class FactoryWithDataprovider { @DataProvider(name = "data2")
public Object[][] createdata() {
return new Object[][] {
{"wangnima"},
{"wangnimei"},
};
} @Factory(dataProvider = "data2")
public Object[] createInstace(String name) {
return new Object[] {
new Test1(name)
};
}
}
执行结果如下:2个实例,每个实例执行4个用例,总共8个用例。
test1: His name is wangnima
test1: His name is wangnimei
test2: wangnima likes baozoumanhua
test2: wangnima likes movie
test2: wangnima likes music
test2: wangnimei likes baozoumanhua
test2: wangnimei likes movie
test2: wangnimei likes music
PASSED: test1
PASSED: test1
PASSED: test2("baozoumanhua")
PASSED: test2("movie")
PASSED: test2("music")
PASSED: test2("baozoumanhua")
PASSED: test2("movie")
PASSED: test2("music")
testng的@Factory及其与@DataProvider的区别的更多相关文章
- TestNG之Factory
如果我们的测试方法中,同一个变量需要很多个不同的测试数据,那么这些测试数据由谁提供呢,testng提供了factory的注解,下面我们来一探究竟. 一.单独使用Factory 1.新建一个含有@Fac ...
- TestNg它@Factory详细解释------如何更改参数值测试
原创文章,版权所有所有.转载,归因:http://blog.csdn.net/wanghantong TestNg的@Factory注解从字面意思上来讲就是採用工厂的方法来创建測试数据并配合完毕測试 ...
- TestNG中@Factory的用法一:简单的数据驱动
为什么要使用@Factory注解呢,先来看下面这个例子 被测试类Person package ngtest; import org.testng.annotations.Parameters; imp ...
- Java自动化测试框架-07 - TestNG之Factory篇 - 欢快畅游梦幻工厂(详细教程)
简介 最近忙着装修博客园,没时间更新文章,今天终于抽出时间把上次写的一半的文章给写完了,新的博客园风格,希望大家喜欢.今天继续介绍testng的相关知识--工厂. 工厂允许你动态的创建测试.例如,假设 ...
- angularjs 中 Factory,Service,Provider 之间的区别
本片文章是使用了 angularjs 中使用 service 在controller 之间 share 对象和数据 的code(http://jsfiddle.net/kn46u0uj/1/) 来进行 ...
- @Factory和@DataProvider的区别
DataProvider: A test method that uses DataProvider will be executed a multiple number of times based ...
- testNG中@Factory详解
@Factory注解从字面意思上来讲就是采用工厂的方法来创建测试数据并配合完成测试,其主要应对的场景是:对于某一个测试用例或方法,我们需要输入多个测试数据进行测试,并且这些测试数据可以是有一定关系(可 ...
- AngularJS 中的 factory、 service 和 provider区别,简单易懂
转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...
- 创建服务factory和service方法的区别
factory方法返回的是对象,json或数组,也可以返回字符串类型的数据,但service方法只能返回数据或对象 创建服务有3种方法 $provide.provider('服务名',function ...
随机推荐
- 常用jar命令
JAR包是Java中所特有一种压缩文档.存储格式格式就是.zip包.但是与ZIP包不同的地方是,生成JAR包时候,会自动添加一个META-INF\MANIFEST.MF文件 命令参数jar {c t ...
- 实例具体解释Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)
这是本系列的第二篇,内容是 prefetch_related() 函数的用途.实现途径.以及用法. 本系列的第一篇在这里 第三篇在这里 3. prefetch_related() 对于多对多字段(Ma ...
- 【BZOJ】1613: [Usaco2007 Jan]Running贝茜的晨练计划(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1613 水题dp 设d[i][j]为i分钟疲劳为j d[i][j]=d[i-1][j-1]+a[i] ...
- 使用命令行操控VirtualBox虚拟机
(1)启动虚拟机:$ VBoxManage startvm <VMNAME> --type gui #执行结束后,就会启动指定的虚拟机,几乎和平时没什么区别. $ VBoxManage ...
- Machine Learning With Spark学习笔记(在10万电影数据上训练、使用推荐模型)
我们如今開始训练模型,还输入參数例如以下: rank:ALS中因子的个数.通常来说越大越好,可是对内存占用率有直接影响,通常rank在10到200之间. iterations:迭代次数,每次迭代都会降 ...
- Redis分布式锁,基于StringRedisTemplate和基于Lettuce实现setNx
使用redis分布式锁,来确保多个服务对共享数据操作的唯一性一般来说有StringRedisTemplate和RedisTemplate两种redis操作模板. 根据key-value的类型决定使用哪 ...
- 编程之美 set 20 构造数独
1. 朴素 DFS 遍历效率太低, 即便是预先设定 9 个数放到数组再去 DFS, 同样并不高效 2. 在生成一个可行解后, 随机删除一些数字, 删除的数字越多, 数独的难度就越大 3. 正解二. 3 ...
- Java Tomcat 启动闪屏-原因之一---配置问题
如Tomcat启动异常,首先确保Java安装和Tomcat安装版本是否对应,环境变量是否配置正确,如检查通过后,依然启动闪屏.可以依次解决: 1.在Tomcat启动文件Startup.bat之中最后添 ...
- Erlang语言学习入门
这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...
- Android 切换主题 (二)
Android 切换主题 (二) 背景 我原来写过一篇文章关于 android 切换主题的文章 -- Android 切换主题以及换肤的实现 , 里面介绍了如何使用 setTheme() 来切换主题, ...