How to disable TestNG test based on a condition

前端 未结 7 1346
挽巷
挽巷 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 07:53

    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(){
    
      }
    }
    

提交回复
热议问题