@Factory注解从字面意思上来讲就是采用工厂的方法来创建测试数据并配合完成测试,其主要应对的场景是:对于某一个测试用例或方法,我们需要输入多个测试数据进行测试,并且这些测试数据可以是有一定关系(可以通过代码控制),此时,我们就可以把自动化或者手动测试时的遇到的只因测试数据不同的多个测试用例合并成一个测试用例,来进行更方便和快捷的测试。

策略:一般我们会在标有@Factory注解的方法中对测试类进行调用,这时TestNg会自动调用测试类中带有@Test注解的方法

配置文件:只需要配置带有@Factory注解的类即可

@Factory必须放在一个返回对象数组的顶部,所有的这些对象都包含测试类的实例,testng会确保@Factory只被调用一次。

@Factory方法是首先被调用的,在@Test方法和配置方法之前,只有当所有的@Factory方法被调用之后,testng才开始执行配置和测试方法。

@Factory允许在运行时动态测试。

简单的使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class SimpleTest {
    @Test
    public void simpleTest1(){
        System.out.println("simple test one");
    }
    @Test
    public void simpleTest2(){
        System.out.println("simple test two");
    }
}
  
public class SimpleTestFactory
{
  @Factory
  public Object[] factoryMethod() {
    return new Object[] { new SimpleTest(), new SimpleTest() };
  }
}

SimpleTestFactory工厂类,在带有@Factory注解的方法中调用被执行的测试类,TestNg会自动调用被执行类中带有@Test注解的方法被执行的测试类为:SimpleTestFactory。

输出结果会:

simple test one

simple test one

simple test two

simple test two

PASSED: simpleTest1

PASSED: simpleTest1

PASSED: simpleTest2

PASSED: simpleTest2

===============================================

Default test

Tests run: 4, Failures: 0, Skips: 0

===============================================

由以上可知所有的test方法都被调用了。

使用@Factory最大的好处就是可以在初始化的时候将参数传给测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class SimpleTest {
    private int para;
    public SimpleTest(int para) {
        this.para = para;
    }
      
    @Test
    public void testMethodOne(){
        int value = para + 1;
        System.out.println("Test method one output: " + value);
    }
    @Test
    public void testMethodTwo(){
        int value = para + 2;
        System.out.println("Test method two output: " + value);
    }
}
 
public class SimpleTestFactory {
     
    @Factory
    public Object[] factoryMethod(){
        return new Object[] { new SimpleTest(0), new SimpleTest(10)};
    }
}

运行SimpleTestFactory,可以得到以下输出:

Test method one output: 1

Test method one output: 11

Test method two output: 2

Test method two output: 12

PASSED: testMethodOne

PASSED: testMethodOne

PASSED: testMethodTwo

PASSED: testMethodTwo

可以知道测试中的每个方法都执行了两遍。

@Factory更适合于同一类型的参数变化性的测试,那么如果参数值没有特定的规律时,我们可以采用@Factory和@DataProvider相结合的方式进行测试

注意要点:测试方法将被一共执行的次数,因为@Factory本身就属于循环测试的类型,@DataProvider也是属于测试整体循环的类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class DataProviderTest
{
  private int param;
   
  @Factory(dataProvider = "dataMethod")
  public DataProviderTest(int param) {
    this.param = param;
  }
   
  @DataProvider
  public static Object[][] dataMethod() {
    return new Object[][] { new Object[]{ 0 }, new Object[]{ 10 } };
  }
   
  @Test
  public void testMethodOne() {
    int opValue = param + 1;
    System.out.println("Test method one output: " + opValue);
  }
   
  @Test
  public void testMethodTwo() {
    int opValue = param + 2;
    System.out.println("Test method two output: " + opValue);
  }
}

dataMethod会返回一个二维数组,维数表示迭代的次数,第二个值表示传入的参数。

使用@Factory的依赖测试,在会先执行所有的依赖方法,然后在执行测试方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class DependencyTest
{
  private int param;
 
  public DependencyTest(int param) {
    this.param = param;
  }
 
  @Test(dependsOnMethods = { "testMethodTwo" })
  public void testMethodOne() {
    System.out.println("Test method one with param values: " this.param);
  }
 
  @Test
  public void testMethodTwo() {
    System.out.println("Test method two with param values: " this.param);
  }
}
 
public class SimpleTestFactory
{
  @Factory
  public Object[] factoryMethod()
  {
    return new Object[] { new DependencyTest(1), new DependencyTest(2) };
  }
}

结果:

Test method two with param values: 2

Test method two with param values: 1

Test method one with param values: 2

Test method one with param values: 1

PASSED: testMethodTwo

PASSED: testMethodTwo

PASSED: testMethodOne

PASSED: testMethodOne

​以上来源于:http://www.tuicool.com/articles/qAzYF3

testNG中@Factory详解的更多相关文章

  1. winxp计算机管理中服务详解

    winxp计算机管理中服务详解01 http://blog.sina.com.cn/s/blog_60f923b50100efy9.html http://blog.sina.com.cn/s/blo ...

  2. cocos2dx常见的46中+22中动作详解

    cocos2dx常见的46中+22中动作详解 分类: iOS2013-10-16 00:44 1429人阅读 评论(0) 收藏 举报 bool HelloWorld::init(){    ///// ...

  3. Android中Context详解 ---- 你所不知道的Context

    转自:http://blog.csdn.net/qinjuning/article/details/7310620Android中Context详解 ---- 你所不知道的Context 大家好,  ...

  4. iOS中-Qutarz2D详解及使用

    在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  5. 【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解

    原文网址:http://www.cnblogs.com/622698abc/p/3348692.html declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs. ...

  6. Python中dict详解

    from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...

  7. 【转】 java中HashMap详解

    原文网址:http://blog.csdn.net/caihaijiang/article/details/6280251 java中HashMap详解 HashMap 和 HashSet 是 Jav ...

  8. java中HashMap详解(转)

    java中HashMap详解 博客分类: JavaSE Java算法JDK编程生活       HashMap 和 HashSet 是 Java Collection Framework 的两个重要成 ...

  9. java集合(2)- java中HashMap详解

    java中HashMap详解 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 H ...

随机推荐

  1. MySQL高可用读写分离方案预研

    目前公司有需求做MySQL高可用读写分离,网上搜集了不少方案,都不尽人意,下面是我结合现有组件拼凑的实现方案,亲测已满足要求,希望各位多提建议 :) 一.    网上方案整理(搜集地址不详...) 1 ...

  2. netty 入门

    先啰嗦两句,使用 netty 来搭建服务器程序,可以发现相比于传统的 nio 程序, netty 的代码更加简洁,开发难度更低,扩展性也很好,非常适合作为基础通信框架. 下面上代码: Server p ...

  3. hdu 1853 Cyclic Tour 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 There are N cities in our country, and M one-way ...

  4. 【ContestHunter】【弱省胡策】【Round2】

    官方题解:http://wyfcyx.is-programmer.com/posts/95490.html A 目前只会30分的暴力……DP好像很神的样子0.0(听说可以多次随机强行算? //Roun ...

  5. 剑指offer--3题

    题目:输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, ...

  6. NYOJ-214 单调递增子序列(二) AC 分类: NYOJ 2014-01-31 08:06 233人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> int len, n, i, j; int d[100005], a[100005]; int bin ...

  7. 【补解体报告】topcoder 634 DIV 2

    A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...

  8. Selenium中expected_conditions下text_to_be_present_in_element_value方法的使用

    text_to_be_present_in_element: 判断某个元素中的text是否包含了预期的字符串 text_to_be_present_in_element_value: 判断某个元素中的 ...

  9. uva 11324

    Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...

  10. POJ 2127

    #include <iostream> #define MAXN 501 using namespace std; int a[MAXN],b[MAXN],ans[MAXN]; int G ...