TestNG Annotation的执行顺序

BeforeSuite>BeforeTest>BeforeClass>BeforeMethod>Test>AfterMethod>AfterClass>AfterTest>AfterSuite

package testng;

import org.testng.annotations.*;

import java.text.SimpleDateFormat;
import java.util.Date;


public class TestAnnotation {

    public String getDateTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date()).toString(); // new Date()为获取当前系统时间
    }

    @BeforeSuite
    public void beforeSuite(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @BeforeTest
    public void beforeTest(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @Test
    public void test001(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @Test
    public void test002(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @AfterMethod
    public void afterMethod(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @AfterClass
    public void afterClass(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @AfterTest
    public void afterTest(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }

    @AfterSuite
    public void afterSuite(){
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()+" : "+ getDateTime());
    }
}

控制台输出

beforeSuite : 2020-04-15 16:37:01
beforeTest : 2020-04-15 16:37:01
beforeClass : 2020-04-15 16:37:01
beforeMethod : 2020-04-15 16:37:01
test001 : 2020-04-15 16:37:01
afterMethod : 2020-04-15 16:37:01
beforeMethod : 2020-04-15 16:37:01
test002 : 2020-04-15 16:37:01
afterMethod : 2020-04-15 16:37:01
afterClass : 2020-04-15 16:37:01
afterTest : 2020-04-15 16:37:01
afterSuite : 2020-04-15 16:37:01

你可能感兴趣的:(TestNG Annotation的执行顺序)