Search code examples
webdrivertestngdataprovider

@DataProvider ignores the @BeforeTest and @AfterTest methods


I have the following code:

@BeforeTest(alwaysRun = true)
public void setup() {
    System.out.println("@BeforeTest");
}

@DataProvider
public String[][] provideData() {
    System.out.println("@DataProvider");
    return new String[][] {
         {"string"},
         {"string2"},
         {"string3"}
    };
}

@Test(dataProvider = "provideData")
public void test(String s) {
    System.out.println("@Test");
    System.out.println(s);
}

@AfterTest(alwaysRun = true)
public void tearDown() {
    System.out.println("@AfterTest");
}

which produces the following output to the console:

@BeforeTest
@DataProvider
@Test
string
@Test
string2
@Test
string3
@AfterTest

I was expecting the @BeforeTest and @AfterTest methods to run before and after the test. I am writing WebDriver tests and would like to setup and teardown after each iteration of the data. What am I missing?


Solution

  • It looks like you're using TestNG, but you're used to JUnit. Here's a table that explains the annotations a bit more.

    http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/