I have seen many answers on how to skip the whole @Test, but I am trying to skip a specific iteration without having to adding extra metadata to dozens of existing tests:
public class TestNgPlayground {
public static final DayOfWeek SOME_CONST = DayOfWeek.TUESDAY;
@DataProvider
public Object[][] getStuff(){
return new Object[][] {
{ "param1", DayOfWeek.MONDAY}, // Skip this
{ "param2", DayOfWeek.TUESDAY }, // Run this
{ "param3", DayOfWeek.WEDNESDAY } // Skip this
};
}
// Ideally I want to detect and Skip in @BeforeMethod / @BeforeClass
@BeforeMethod // testArgs fetches the DataProvider values
public void setUp(Object[] testArgs,ITestResult result ){
DayOfWeek dataProviderValue = (DayOfWeek) testArgs[1];
if(dataProviderValue!= SOME_CONST ) {
// throw new SkipException("skip test"); // No good - skips the entire Test
// result.setStatus(ITestResult.SKIP); // Doesn't do anything
}
}
@Test(dataProvider = "getStuff")
public void testt(String param1, DayOfWeek param2){
// some testing
}
// 2nd best option - overwrite to Skip after test has run,
// though time has been wasted
@AfterMethod
public void tearDown(ITestResult result){
DayOfWeek dow = (DayOfWeek) result.getParameters()[1];
if(dow != SOME_CONST){
result.setStatus(ITestResult.SKIP); // Doesn't do anything either... ?
}
}
}
For the above example, I want the final report to show:
Iteration 1 - Skipped
Iteration 2 - Passed
Iteration 3 - Skipped