Fixture 是指一个测试运行所需的固定环境,通俗来说 ,为测试用例所准备的环境。
以下是TestNG中可用的注释及其属性的简要概述。

我们先演示@BeforeClass、@AfterClass、@BeforeMethod、@AfterMethod 四个注解
import org.testng.annotations.*;
public class TestFixture {
    //在当前测试类开始时运行。
    @BeforeClass
    public static void beforeClass(){
        System.out.println("-------------------beforeClass");
    }
    //在当前测试类结束时运行。
    @AfterClass
    public static void afterClass(){
        System.out.println("-------------------afterClass");
    }
    //每个测试方法运行之前运行
    @BeforeMethod
    public void before(){
        System.out.println("=====beforeMethod");
    }
    //每个测试方法运行之后运行
    @AfterMethod
    public void after(){
        System.out.println("=====afterMethod");
    }
    @Test
    public void testCase1(){
        System.out.println("test case 1");
    }
    @Test
    public void testCase2(){
        System.out.println("test case 2");
    }
}
查看运行结果:

原文:https://www.cnblogs.com/suim1218/p/8856199.html