首页 > 技术文章 > 注解应用

yitao326 2022-01-22 20:34 原文


@BeforeMethod:是在测试方法之前运行,每执行一条用例都会运行一次
@AfterMethod:是在测试方法之后运行,每执行一条用例都会运行一次
@BeforeClass:是在类运行之前运行的,只会执行一次
@AfterClass:是在类运行之后运行的,只会执行一次
@BeforeSuite:是在测试套件运行之前运行的,只会执行一次
@AfterSuite:是在测试套件运行之后运行的,只会执行一次
@Test:执行测试用例
@Test(enabled = false):跳过该条测试用例,不加enabled默认为true
import org.testng.annotations.*;

public class Demo2 {
    @BeforeMethod
    public void testCase01(){
        System.out.println("BeforeMethod是在测试方法之前用的");
    }

    @Test
    public void testCase02(){
        System.out.println("这是一条测试用例1");
    }

    @Test
    public void testCase03(){
        System.out.println("这是一条测试用例2");
    }

    @AfterMethod
    public void testCase04(){
        System.out.println("AfterMethod是在测试方法之后用的");
    }

    @BeforeClass
    public void testCase05(){
        System.out.println("BeforeClass是在类运行之前运行的");
    }

    @AfterClass
    public void testCase06(){
        System.out.println("BeforeClass是在类运行之后运行的");
    }

    @BeforeSuite
    public void testCase07(){
        System.out.println("BeforeSuite是在测试套件之前运行的");
    }

    @AfterSuite
    public void testCase08(){
        System.out.println("AfterSuite是在测试套件之后运行的");
    }
}

 

推荐阅读