TestNg: When will @BeforeClass, @BeforeMethod, @BeforeTest be run and in what sequence?

public class KentNg {


    @DataProvider(name = "someData")
    public Object[][] someData() {
        return new Object[][]{
                {"a1", "b1"},
                {"a2", "b2"}
        };
    }

    @BeforeTest
    public void setupBeforeTest() {
        System.out.println("setupBeforeTest() is run");
    }


    @BeforeClass
    public void setupBeforeClass() {
        System.out.println("setupBeforeClass() is run");
    }

    @BeforeMethod
    public void setupBeforeMethod() {
        System.out.println("setupBeforeMethod() is run");
    }

    @Test(dataProvider = "someData")
    public void foo(String a, String b) {
        System.out.println("foo() is run");
    }


    @Test(dataProvider = "someData")
    public void bar(String a, String b) {

        System.out.println("bar() is run");
    }
}

And output is

setupBeforeTest() is run
setupBeforeClass() is run
setupBeforeMethod() is run
bar() is run
setupBeforeMethod() is run
bar() is run
setupBeforeMethod() is run
foo() is run
setupBeforeMethod() is run
foo() is run

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.