zoukankan      html  css  js  c++  java
  • testNG之组测试

    @Test(groups = {""})

      在执行测试用例的时候,往往一个功能依赖多个测试用例,比如流程的测试,那么这个时候就可以用到组测试,把流程涉及到测试用例都分到同一组里,按组执行即可。

      testNG的组通过@Test的groups属性来指定的,一个方法可以属于一个组(@Test(groups = {"checkintest"})),也可以属于多个组(@Test(groups = {"functest","checkintest"}))。

      假设现在有3个java代码,common.java、functionA.java、functionB.java,测试流程涉及到common.java中 login() 和 quit() 方法、functionA.java中的 testMethod1() 方法、functionB.java中的 testMethod3() 方法,那么可以将这4个方法分到同一组里functest,代码如下:

    common.java:

    import org.testng.annotations.Test;
    
    public class common {
        @Test(groups = {"functest","checkintest"})
        public void login() {
            System.out.println("login");
        }
        
        @Test(groups = {"functest","checkintest"})
        public void quit() {
            System.out.println("quit");
        }
        
        @Test(groups = {"checkintest"})
        public void init() {
            System.out.println("init");
        }
    }

    functionA.java:

    import org.testng.annotations.Test;
    
    
    public class functionA {
        @Test(groups = {"functest"})
        public void testMethod1() {
            System.out.println("this is testMethod1");        
        } 
        
        @Test(groups = {"functest2"})
        public void testMethod2() {
            System.out.println("this is testMethod2");        
        } 
    }

    functionB.java:

    import org.testng.annotations.Test;
    
    public class functionB {
        @Test(groups = {"functest"})
        public void testMethod3() {
            System.out.println("this is testMethod3");        
        } 
        
        @Test(groups = {"functest2"})
        public void testMethod4() {
            System.out.println("this is testMethod4");        
        } 
    }

    testng.xml:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
    <suite name="Suite1" verbose="1" >
        <test name="Regression1" preserve-order="true">
            <groups>
                <run>
                    <include name = "functest" />
                </run>        
            </groups>
            <classes>
                <class name="common"></class>
                <class name="functionA"></class>
                <class name="functionB"></class>
            </classes>
        </test>
    </suite>

    注意:testng.xml需要指定要测试的组(<groups>……</groups>)和组所在的class(<classes>……</classes>)

    运行testng.xml,执行结果如下:

    login
    quit
    this is testMethod1
    this is testMethod3


    @Test(priority = )

    一般quit都是在流程的最后才执行,如何控制组里方法执行的顺序呢?可以通过@Test的priority属性,testNG按照priority从小到大的顺序执行

    修改common.java,在@Test中添加属性priority = 1priority = 4

    import org.testng.annotations.Test;
    
    public class common {
        @Test(groups = {"functest","checkintest"},priority = 1)
        public void login() {
            System.out.println("login");
        }
        
        @Test(groups = {"functest","checkintest"},priority = 4)
        public void quit() {
            System.out.println("quit");
        }
        
        @Test(groups = {"checkintest"})
        public void init() {
            System.out.println("init");
        }
    }

    修改functionA.java,在@Test中添加属性priority = 2

    import org.testng.annotations.Test;
    
    
    public class functionA {
        @Test(groups = {"functest"},priority = 2)
        public void testMethod1() {
            System.out.println("this is testMethod1");        
        } 
        
        @Test(groups = {"functest2"})
        public void testMethod2() {
            System.out.println("this is testMethod2");        
        } 
    }

    修改functionB.java,在@Test中添加属性priority = 3

    import org.testng.annotations.Test;
    
    public class functionB {
        @Test(groups = {"functest"}, priority = 3)
        public void testMethod3() {
            System.out.println("this is testMethod3");        
        } 
        
        @Test(groups = {"functest2"})
        public void testMethod4() {
            System.out.println("this is testMethod4");        
        } 
    }

    再次运行testng.xml,执行结果如下:

    login
    this is testMethod1
    this is testMethod3
    quit

    如何只执行组里 testMethod1() 和 testMethod3() 方法,而不执行 login() 和 quit() 方法呢?

    修改testng.xml,添加<exclude name="checkintest"></exclude>,排除在checkintest组里的方法

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
    <suite name="Suite1" verbose="1" >
        <test name="Regression1" preserve-order="true">
            <groups>
                <run>
                    <include name = "functest" />
                    <exclude name="checkintest"></exclude>
                </run>        
            </groups>
            <classes>
                <class name="common"></class>
                <class name="functionA"></class>
                <class name="functionB"></class>
            </classes>
        </test>
    </suite>

    再次运行testng.xml,执行结果如下:

    this is testMethod1
    this is testMethod3



  • 相关阅读:
    2021-04-02:给定一个正方形或者长方形矩阵matrix,实现zigzag打印。[[0,1,2],[3,4,5],[6,7,8]]的打印顺序是0,1,3,6,4,2,5,7,8。
    2021-04-01:给定一个正方形矩阵matrix,原地调整成顺时针90度转动的样子。[[a,b,c],[d,e,f],[g,h,i]]变成[[g,d,a],[h,e,b],[i,f,c]]。
    2021-03-31:给定一个数组arr,给定一个值v。求子数组平均值小于等于v的最长子数组长度。
    2021-03-30:给定一个整数组成的无序数组arr,值可能正、可能负、可能0。给定一个整数值K,找到arr的所有子数组里,哪个子数组的累加和<=K,并且是长度最大的。返回其长度。
    2021-03-29:无序数组arr,子数组-1和1的数量一样多,请问最长子数组的长度是多少?
    04Null和Undefined
    03数据类型
    win10 命令行下 重启虚拟网卡
    JavaScript注释及命名规范
    第一个javascrpt代码
  • 原文地址:https://www.cnblogs.com/yuan-yuan/p/4498134.html
Copyright © 2011-2022 走看看