TestNG 组测试

方法分组测试

1. 给@Test注解后面加groups参数,如

@Test(groups = "groupa")

2. 可以添加@BeforeGroups和@AfterGroups,来设置在某组方法执行之前/之后要做的事情,如

@BeforeGroups("groupa")
 1 import org.testng.annotations.AfterGroups;
 2 import org.testng.annotations.BeforeGroups;
 3 import org.testng.annotations.Test;
 4 
 5 public class GroupOnMethod {
 6     @Test(groups = "groupa")
 7     public void test1(){
 8         System.out.println("A group--->this is test 1");
 9     }
10 
11     @Test(groups = "groupa")
12     public void test2(){
13         System.out.println("A group--->this is test 2");
14     }
15 
16     @Test(groups = "groupb")
17     public void test3(){
18         System.out.println("b group--->this is test 2");
19     }
20 
21     @Test(groups = "groupb")
22     public void test4(){
23         System.out.println("b group--->this is test 2");
24     }
25 
26     @BeforeGroups("groupa")
27     public void beforeGroups1(){
28         System.out.println("Run before A group");
29     }
30 
31     @AfterGroups("groupa")
32     public void afterGroups1(){
33         System.out.println("Run after A group");
34     }
35 
36 }

3. 设置testng XML文件。 (直接运行上面的java文件,4个测试方法均会执行,分组功能不生效)

<include>标签中设置要执行的group名称。

<?xml version="1.0" encoding="utf-8" ?>
<suite name="test">
    <test name="group">
        <groups>
            <run>
                <include name="groupa"/>
            </run>
        </groups>
        <classes>
            <class name="com.course.testng.groups.GroupOnMethod"/>
        </classes>
    </test>

</suite>

4. 执行XML文件。

Run before A group

A group--->this is test 1

A group--->this is test 2

Run after A group

===============================================
test
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

 

类分组测试

1. 给类上面加@Test(groups='xxx')注解

import org.testng.annotations.Test;

@Test(groups="c1")
public class GroupsOnClass1 {

    public void f1(){
        System.out.println("class 1 method run ");
    }
}



@Test(groups="c1")
public class GroupsOnClass2 {
    public void f1(){
        System.out.println("class 2 method run");
    }
}



@Test(groups="n")
public class GroupsOnClass3 {
    public void f1(){
        System.out.println("class 3 method run");
    }
}

2. 配置XML文件

<?xml version="1.0" encoding="UTF-8" ?>

<suite name="suitename">
    <test name="rungroups">
        <classes>
            <class name="com.course.testng.groups.GroupsOnClass1"/>
            <class name="com.course.testng.groups.GroupsOnClass3"/>
            <class name="com.course.testng.groups.GroupsOnClass2"/>
        </classes>
        <groups>
            <run>
                <include name="c1"/>
            </run>
        </groups>
    </test>
</suite>

3.执行XML文件(只有跟include标签中name相同的groups类下的方法被执行)

class 1 method run 

class 2 method run

===============================================
suitename
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

 

posted @ 2021-05-02 12:02  qiyue1118  阅读(108)  评论(0编辑  收藏  举报