Balaraj V
Balaraj V

Reputation: 63

Skip all test cases in a class based on condition in TestNG

I have a condition in which I need to skip the test cases inside a class based on a condition in @BeforeClass. I know, this can be achieved by throwing skip exception in @BeforeClass

Class someClass {

      @BeforeClass
      public void beforeclass() {
           if(someThing.equals("Wrong")) {
                  throw new SkipException("Testing skip.");
           }
      }

      @Test
      public void test_method1() {
            //will be skipped if something is wrong.  
      }

      @Test
      public test_method2() {
           //will be skipped if something is wrong.
      }         
}

The problem with this approach is I need to write @BeforeClass method in each and every test class I have written. Is there a listener for class invocation? or Is there a way to write one global beforeClass which has access to class name which is will call this method? I could not find anything for class invocation listener. Any other approach to solve this issue is also appreciated.

Upvotes: 1

Views: 1685

Answers (1)

juherr
juherr

Reputation: 5740

IClassListener should help you.

Upvotes: 1

Related Questions