ssharma
ssharma

Reputation: 941

TestNG: Is it possible to use DataProvider in AfterTest(), AfterClass(), AfterMethod()?

I am writing few test in which I am using DataProvider for @Test and creating few thing now as a cleanup/teardown() step I want to delete these things in @After methods, how can I use DataProvider in AfterTest(), AfterClass(), AfterMethod() ?

Upvotes: 0

Views: 775

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

It is possible. For example TestNg can inject the same objects to @AfterMethod. See the example below:

@DataProvider(name = "test")
public Object[][] testDataProvide(){
    return new Object[][]{
            {"11", "12"},
            {"21", "22"}
    };
}

@Test(dataProvider = "test")
public void testDP(String one, String two){
    System.out.println(String.join(",", one, two));
}

@AfterMethod
public void tearDownEach(Object[] args){
    System.out.println("Tearing down: " + String.join(",", args[0].toString(), args[1].toString()));
}

P.S. - AfterTest() and AfterClass() do not have such way since they run after a bunch of tests have completed which does not make sense to use with data provider that is intended to supply a piece of data to every single test.

Upvotes: 0

Related Questions