【java测试-testng3】分组测试的两种形式:方法分组和类分组

对于注解的生成,都是基于实际的需求,比如之前讲到的 @Test(timeOut = 4000) 和 @Test(enabled = false)。

分组测试的产生,也是基于一定的需求背景。

 

第一种:同一个测试类下,部分测试方法需要特殊处理,加前置、后置操作等。

package com.coke.testng;

import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;

/*
分组测试
 */

public class GroupsTest {

    @Test(groups = "teacher")
    public void test1(){
        System.out.println("这是 teacher 的测试用例1");
    }

    @Test(groups = "teacher")
    public void test2(){
        System.out.println("这是 teacher 的测试用例2");
    }

    @Test(groups = "student")
    public void test3(){
        System.out.println("这是 student 的测试用例3");
    }

    @Test(groups = "student")
    public void test4(){
        System.out.println("这是 student 的测试用例4");
    }

    @BeforeGroups("teacher")
    public void beforeGroupsOnTeacher(){
        System.out.println("beforeGroupsOnTeacher()...");
    }

    @AfterGroups("teacher")
    public void afterGroupsOnTeacher(){
        System.out.println("afterGroupsOnTeacher()...");
    }
    
}

 运行结果如下:

beforeGroupsOnTeacher()...

这是 teacher 的测试用例1

这是 teacher 的测试用例2

afterGroupsOnTeacher()...

这是 student 的测试用例3

这是 student 的测试用例4

===============================================
Default Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

 

第二种:测试类上加注释,并且通过xml文件实现分组

package com.coke.testng.suite;

import org.testng.annotations.Test;

/*
第一个测试类
为了便于分组测试
*/ @Test(groups = "stu") public class StuTest { public void test1(){ System.out.println("StuTest 中的 test1运行了"); } }
package com.coke.testng.suite;

import org.testng.annotations.Test;

/*
第二个测试类
 */

@Test(groups = "tea")
public class Teatest {

    public void test1(){
        System.out.println("Teatest 中的 test1 运行了");
    }
}

xml文件中,<groups>标签中可以注明要运行哪些组

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

    <test name="stu">
        <groups>
            <run>
                <include name="stu" />
            </run>
        </groups>
        <classes>
            <class name="com.coke.testng.suite.SuiteConfig" />
            <class name="com.coke.testng.suite.StuTest" />
            <class name="com.coke.testng.suite.Teatest" />
        </classes>
    </test>

</suite>

运行xml的结果:

StuTest 中的 test1运行了

===============================================
test
Total tests run: 1, Failures: 0, Skips: 0
===============================================

 

posted @ 2021-01-11 09:43  愚人李愚  阅读(371)  评论(0编辑  收藏  举报