• testNG-@Factory详解+demo


    @Factory详解

    简介

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

    对编写自己主动化测试代码人员节省了非常多时间

    策略:

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

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

    1. @Factory必须放在一个返回对象数组的顶部,所有的这些对象都包含测试类的实例,testng会确保@Factory只被调用一次;
    2. @Factory方法是首先被调用的,在@Test方法和配置方法之前,只有当所有的@Factory方法被调用之后,testng才开始执行配置和测试方法;
    3. @Factory允许在运行时动态测试。

    上边说了这么多是不是把大家说的云里雾里,晕头转向的,接下来通过具体的例子给小伙伴和同学们分享一下。

    实例:

    被测试类Person:

    publicclass Person {
    
        String name;
    
        int age;
    
    
    
        @Parameters({"name","age"})
    
        publicPerson(String name,int age) {
    
            super();
    
            this.name = name;
    
            this.age = age;
    
        }
    
        @Test()
    
        publicvoid say() {
    
            System.out.print("我是"+name+" ");
    
            if(age<18){
    
                System.out.println("我未成年");
    
            }elseif(age>=18&&age<=45){
    
                System.out.println("我是青年人");
    
            }elseif(age>45&&age<=60){
    
                System.out.println("我是中年人");
    
            }elseif(age>60){
    
                System.out.println("我是老年人");
    
            }
    
        }
    
    }
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    该类的say()方法中有四个判断分支,为了测试充分,必须执行四次这个方法,如果不使用@Factory注解,在TestNG配置文件中必须这样配置:

    
    
    DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    
    <suite name="Suite">
    
      <test name="Test1">
    
            <parameter name="name" value="小明" />
    
            <parameter name="age" value="10" />
    
            <classes>
    
                <class name="hongge.Person" />
    
            classes>
    
        test>
    
        <test name="Test2">
    
            <parameter name="name" value="宏哥" />
    
            <parameter name="age" value="20" />
    
            <classes>
    
                <class name="hongge.Person" />
    
            classes>
    
        test>
    
        <test name="Test3">
    
            <parameter name="name" value="刘创" />
    
            <parameter name="age" value="50" />
    
            <classes>
    
                <class name="hongge.Person" />
    
            classes>
    
        test>
    
        <test name="Test4">
    
            <parameter name="name" value="爷爷" />
    
            <parameter name="age" value="70" />
    
            <classes>
    
                <class name="hongge.Person" />
    
            classes>
    
        test>
    
    suite>
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    从上边我们可以清楚地看出来:参数一旦多起来,就难以管理了,所以应该使用工厂来做

    工厂

    Factory注解

    如果使用@Factory注解,就比较简单,而且方便扩展,示例如下。

    不需改动原有类,添加一个新类PersonFactory

    publicclass PersonFactory {
    
        @Factory
    
        public Object[] factory() {
    
            ArrayList testList =newArrayList<>();
    
            Person tp =newPerson("明明",10);
    
            testList.add(tp);
    
            Person tp2 =newPerson("宏哥",20);
    
            testList.add(tp2);
    
            Person tp3 =newPerson("刘创",50);
    
            testList.add(tp3);
    
            Person tp4 =newPerson("朱爷爷",70);
    
            testList.add(tp4);
    
            return testList.toArray();
    
        }
    
    }
    
    • 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
    • 28
    • 29
    
    
    DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    
    <suite name="Suite">
    
      <test name="Test1">
    
            <classes>
    
                <class name="hongge.PersonFactory" />
    
            classes>
    
        test>
    
    suite> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    使用@Factory的运行原理

    1、如果不使用@Factory,运行普通的被@Test标注的方法时,实际上是TestNG框架调用了该类的构造函数构造出一个对象,然后再执行对象的这个方法。

    2、使用了@Factory后,可以看到被@Factory标注的方法返回了一个Object数组,数组中每一个元素是一个被测试类的对象。也就是说@Factory构造了多个被测试类对象,然后把每一个对象都传递给了TestNG框架,然后TestNG框架在分别执行这些对象中被@Test标注的方法。

  • 相关阅读:
    聚观早报 | 苹果秋季发布会定档9月7日;​饿了么与抖音达成合作
    【Java 进阶篇】Java Request 请求转发详解
    Socks5与HTTP的区别与应用场景
    Linux进程基础(一)
    第二部分:关键技术领域的开源实践【内网穿透FRP】
    jvm server和client模式切换
    数据结构刷题——二叉树
    机器学习编译器的前世今生
    面渣逆袭:MySQL六十六问,两万字+五十图详解
    html给下拉框添加搜索、分页功能(通过ajax从服务器获取搜索数据)
  • 原文地址:https://blog.csdn.net/weixin_42439274/article/details/132642547