How to disable TestNG test based on a condition

Afamee picture Afamee · Oct 15, 2010 · Viewed 37.8k times · Source

Is there currently a way to disable TestNG test based on a condition

I know you can currently disable test as so in TestNG:

@Test(enabled=false, group={"blah"})
public void testCurrency(){
...
}

I will like to disable the same test based on a condition but dont know how. something Like this:

@Test(enabled={isUk() ? false : true), group={"blah"})
public void testCurrency(){
...
}

Anyone has a clue on whether this is possible or not.

Answer

Bruce picture Bruce · Dec 13, 2010

An easier option is to use the @BeforeMethod annotation on a method which checks your condition. If you want to skip the tests, then just throw a SkipException. Like this:

@BeforeMethod
protected void checkEnvironment() {
  if (!resourceAvailable) {
    throw new SkipException("Skipping tests because resource was not available.");
  }
}