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



原文地址:https://www.cnblogs.com/yuan-yuan/p/4498134.html