1

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

Is it guaranteed @BeforeClass executes before @DataProvider?

Krishna Majgaonkar
  • 1,532
  • 14
  • 25
Akshay
  • 63
  • 5

2 Answers2

1

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
Gautham M
  • 4,816
  • 3
  • 15
  • 37
Krishna Majgaonkar
  • 1,532
  • 14
  • 25
0

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.

learningIsFun
  • 142
  • 1
  • 9