zoukankan      html  css  js  c++  java
  • TestNG之Factory

    如果我们的测试方法中,同一个变量需要很多个不同的测试数据,那么这些测试数据由谁提供呢,testng提供了factory的注解,下面我们来一探究竟。

    一、单独使用Factory

    1.新建一个含有@Factory的类,且在该类中实例化测试类的对象,如

    package com.testng;
    
    import org.testng.annotations.Factory;
    
    /** 
     * @author QiaoJiafei 
     * @version 创建时间:2015年12月22日 下午6:17:06 
     * 类说明 
     */
    public class TestFactory {
    
         @Factory
         public Object[] createInstances() {
             Object[] result = new Object[10]; 
             for (int i = 0; i < 10; i++) {
             result[i] = new WebTest(i * 10);
             }
             return result;
          }
            
    }

    2.新建测试类,并使用这个工厂类提供的数据

    package com.testng;
    
    import org.testng.annotations.Test;
    
    /** 
     * @author QiaoJiafei 
     * @version 创建时间:2015年12月22日 下午6:19:10 
     * 类说明 
     */
    public class WebTest {
          private int m_numberOfTimes;
          public WebTest(int numberOfTimes) {
            m_numberOfTimes = numberOfTimes;
          }
         
          @Test
          public void testServer() {
           // access the web page
           System.out.println(m_numberOfTimes ); 
      }
    }

    3.在xml文件中只需要,增加<class name="com.testng.TestFactory"/>即可,注意不需要在xml中在额外定义<class name="com.testng.WebTest"/>。

    4.执行结果,打印:

    30
    10
    0
    80
    70
    60
    20
    40
    50
    90

    5.执行后,报告显示如下:

    相当于运行了10次测试方法。The factory method can receive parameters just like @Test and @Before/After and it must return Object[].  The objects returned can be of any class (not necessarily the same class as the factory class) and they don't even need to contain TestNG annotations (in which case they will be ignored by TestNG).

    二、Factory和dataProvider 配合使用

    package com.testng;
    
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Factory;
    
    /** 
     * @author QiaoJiafei 
     * @version 创建时间:2015年12月22日 下午6:17:06 
     * 类说明 
     */
    public class TestFactory {
    
         /*@Factory
         public Object[] createInstances() {
             Object[] result = new Object[10]; 
             for (int i = 0; i < 10; i++) {
                 result[i] = new WebTest(i * 10);
             }
             return result;
          }*/
         @Factory(dataProvider = "dp")
         public Object[] createInstances(int n) {
             Object[] result = new Object[1];
             for (int i = 0; i < 1; i++) {
                 result[i] = new WebTest(n);
             }
             return result;
         }
          
         @DataProvider
         static public Object[][] dp() {
           return new Object[][] {
             new Object[] { 41 },
             new Object[] { 42 },
           };
         }
    
            
    }

    执行结果,打印:

    41

    42

    报告显示执行了两次测试方法

  • 相关阅读:
    mssql 循环的写法,备用
    用了十几年的windows记录下我不知道的几个快捷键
    折腾了下java下webservice,折腾了大半天,居然是eclipse的版本不对
    连接Linux 下mysql 慢的问题,解决之
    解决windows7蓝屏的方法
    MySQL錯誤:Value '00000000' can not be represented as java.sql.Date解決方法[转]
    jdbc连接三种数据库的连接语句写法(备查)
    遇到一个json解析的错误,费了好大的劲,最后发现是少了一个包
    【转】The reference to entity "characterEncoding" must end with the ';' delimiter
    synaptics 插入USB鼠标禁用,网上
  • 原文地址:https://www.cnblogs.com/qiaoyeye/p/5067778.html
Copyright © 2011-2022 走看看