How to disable TestNG test based on a condition

前端 未结 7 1336
挽巷
挽巷 2020-12-05 07:23

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(en         


        
7条回答
  •  粉色の甜心
    2020-12-05 08:02

    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:

    
    

提交回复
热议问题