27

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.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Afamee
  • 5,160
  • 9
  • 38
  • 43
  • Annotations aren't executable code, so this is unlikely. What are you really trying to do - in what conditions would you want a test to be run or not run? – matt b Oct 15 '10 at 20:32
  • thanks matt. See cedrics answer below for more details. – Afamee Oct 15 '10 at 23:53

7 Answers7

39

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.");
  }
}
Bruce
  • 391
  • 1
  • 2
  • 2
  • 1
    Hi, since 6.13 throwing SkipException sets status to Failed instead of Skipped. See https://stackoverflow.com/questions/20022288/conditional-skipping-of-testng-tests/47571922#47571922 – Mike G. Nov 30 '17 at 11:18
  • The documentation of [SkipException](https://static.javadoc.io/org.testng/testng/6.8.1/org/testng/SkipException.html) refers to the deprecated '@Configuration annotation. This might explain the described behavior for '@BeforeMethod, etc. – dpr Apr 26 '19 at 09:58
  • Doesn't `enabled=false` check occur at an even earlier stage of the test than BeforeMethod? In some cases, using `enabled=false` would be better than using Before-hook, IMHO. – djangofan May 15 '19 at 19:52
16

You have two options:

Your annotation transformer would test the condition and then override the @Test annotation to add the attribute "enabled=false" if the condition is not satisfied.

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55
  • Thanks cedric. I think i will like to explore the 'annotation transformer' option. that sounds more like what am looking for. – Afamee Oct 15 '10 at 22:28
  • Thanks again. It didnt take me long to get a working example of this transformer. One thing though is not behaving as i expected. I want to dynamically transform the name of the test that i run (...at least the way it will be displayed on the test result) by calling annot.setTestName(concatString) where annot represents the method annotation but result comes back with original name unchanged. Is there another way to do this?? Hopefully didnt confuse u. – Afamee Oct 15 '10 at 23:45
  • You won't be able to override the behavior of a class at runtime, that's just how Java is designed. Instead, I suggest you put the logic that decides what that name is directly into the test so you can return it in getTestName(). – Cedric Beust Oct 16 '10 at 01:45
  • that is exactly what I did. I thought setTestName("newName") was meant to change the name of the test. I understand calling getTestName() to get name based on logic in test code but then I want to set this newly retrieved name for the test by saying annot.setTestName(newName) or annot.setTestName(getTestName()+"_Modified"). When the test completes, it still has the original name and not the modified name. – Afamee Oct 16 '10 at 15:24
  • Can this be achieved? - I want to disable **TestB** if **TestA** has failed. – Rameshwar Sep 06 '17 at 07:33
10

There are two ways that I know of that allow you the control of "disabling" tests in TestNG.

The differentiation that is very important to note is that SkipException will break out off all subsequent tests while implmenting IAnnotationTransformer uses Reflection to disbale individual tests, based on a condition that you specify. I will explain both SkipException and IAnnotationTransfomer.

SKIP Exception example

import org.testng.*;
import org.testng.annotations.*;

public class TestSuite
{
    // You set this however you like.
    boolean myCondition;
    
    // Execute before each test is run
    @BeforeMethod
    public void before(Method methodName){
        // check condition, note once you condition is met the rest of the tests will be skipped as well
        if(myCondition)
            throw new SkipException();
    }
    
    @Test(priority = 1)
    public void test1(){}
    
    @Test(priority = 2)
    public void test2(){}
    
    @Test(priority = 3)
    public void test3(){}
}

IAnnotationTransformer example

A bit more complicated but the idea behind it is a concept known as Reflection.

Wiki - http://en.wikipedia.org/wiki/Reflection_(computer_programming)

First implement the IAnnotation interface, save this in a *.java file.

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class Transformer implements IAnnotationTransformer {

    // Do not worry about calling this method as testNG calls it behind the scenes before EVERY method (or test).
    // It will disable single tests, not the entire suite like SkipException
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){

        // If we have chose not to run this test then disable it.
        if (disableMe()){
            annotation.setEnabled(false);
        }
    }

    // logic YOU control
    private boolean disableMe() {
    }
}

Then in you test suite java file do the following in the @BeforeClass function

import org.testng.*;
import org.testng.annotations.*;

/* Execute before the tests run. */    
@BeforeClass
public void before(){

    TestNG testNG = new TestNG();
    testNG.setAnnotationTransformer(new Transformer());
}

@Test(priority = 1)
public void test1(){}

@Test(priority = 2)
public void test2(){}

@Test(priority = 3)
public void test3(){}

One last step is to ensure that you add a listener in your build.xml file. Mine ended up looking like this, this is just a single line from the build.xml:

<testng classpath="${test.classpath}:${build.dir}" outputdir="${report.dir}" 
    haltonfailure="false" useDefaultListeners="true"
    listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,Transformer" 
    classpathref="reportnglibs"></testng>
Makeen A. Sabree
  • 275
  • 3
  • 11
3

A Third option also can be Assumption Assumptions for TestNG - When a assumption fails, TestNG will be instructed to ignore the test case and will thus not execute it.

  • Using the @Assumption annotation
  • Using AssumptionListener Using the Assumes.assumeThat(...) method

You can use this example: Conditionally Running Tests In TestNG

Laurel
  • 5,965
  • 14
  • 31
  • 57
avivamg
  • 12,197
  • 3
  • 67
  • 61
3

I prefer this annotation based way for disable/skip some tests based on environment settings. Easy to maintain and not requires any special coding technique.

  • Using the IInvokedMethodListener interface
  • Create a custom anntotation e.g.: @SkipInHeadlessMode
  • Throw SkipException
public class ConditionalSkipTestAnalyzer implements IInvokedMethodListener {
    protected static PropertiesHandler properties = new PropertiesHandler();

    @Override
    public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult result) {
        Method method = result.getMethod().getConstructorOrMethod().getMethod();
        if (method == null) {
            return;
        }
        if (method.isAnnotationPresent(SkipInHeadlessMode.class)
                && properties.isHeadlessMode()) {
            throw new SkipException("These Tests shouldn't be run in HEADLESS mode!");
        }
    }

    @Override
    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        //Auto generated
    }
}

Check for the details: https://www.lenar.io/skip-testng-tests-based-condition-using-iinvokedmethodlistener/

0

Throwing a SkipException in a method annotated with @BeforeMethod did not work for me because it skipped all the remaining tests of my test suite with no regards if a SkipException were thrown for those tests.

I did not investigate it thoroughly but I found another way : using the dependsOnMethods attribute on the @Test annotation:

import org.testng.SkipException;
import org.testng.annotations.Test;

public class MyTest {

  private boolean conditionX = true;
  private boolean conditionY = false;

  @Test
  public void isConditionX(){
    if(!conditionX){
      throw new SkipException("skipped because of X is false");
    }
  }

  @Test
  public void isConditionY(){
    if(!conditionY){
      throw new SkipException("skipped because of Y is false");
    }
  }

  @Test(dependsOnMethods="isConditionX")
  public void test1(){

  }

  @Test(dependsOnMethods="isConditionY")
  public void test2(){

  }
}
letrait
  • 11
  • 2
0

SkipException: It's useful in case of we have only one @Test method in the class. Like for Data Driven Framework, I have only one Test method which need to either executed or skipped on the basis of some condition. Hence I've put the logic for checking the condition inside the @Test method and get desired result. It helped me to get the Extent Report with test case result as Pass/Fail and particular Skip as well.

PAQuality101
  • 11
  • 1
  • 4