Akshay
Akshay

Reputation: 133

Do methods annotated with @BeforeClass and @BeforeSuite execute before @DataProvider in TestNG?

I want to use @BeforeClass to do some test data setup and use it in @DataProvider.

Is it guaranteed @BeforeClass executes before @DataProvider?

Upvotes: 1

Views: 1147

Answers (2)

learningIsFun
learningIsFun

Reputation: 152

Yes it is guaranteed that @BeforeClass executes before @DataProvider. @BeforeClass is seen by the compiler at class level whereas @DataProvider is seen by the compiler at method level.

Upvotes: 0

Krishna Majgaonkar
Krishna Majgaonkar

Reputation: 1726

Yes, @BeforeClass and @BeforeSuite will get executed before @DataProvider in TestNG.

You can refer testNG documetation

@BeforeSuite: The annotated method will be run before all tests in this suite have run.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

Though it doesn't give a clear idea whether it will execute before @DataProvider or not, I have created sample test:

public class SeleniumJava {
    Object[][] testData;

    @DataProvider
    public Object[][] data() {
        System.out.println("In @DataProvider");
        return testData;
    }

    @BeforeClass
    public void setData() {
        System.out.println("in @BeforeClass");
        testData = new String[][]{new String[]{"data1"}, new String[]{"data2"}};
    }

    @Test(dataProvider = "data")
    public void printData(String d) {
        System.out.println(d);
    }
}

Output:

in @BeforeClass
In @DataProvider
data1
data2

Upvotes: 1

Related Questions