简体   繁体   中英

TestNG how to skip test when beforetest fails in inheritance

I have a little problem with testNG. Below my example code:

abstract class parent {

 abstract void beforeTest();

 @Test
 void test() {
         // some testing
 }
}

class child extends parent {

 @BeforeTest
 void beforeTest() {
      \\some before things
 }
}

And the question is how to do this code work properly? So I want to perform beforeTest() method and if it fails the test method shoud skip. How can I achieve this thing?

Usually, the configuration methods go into the parrent and test classes should extend the parrent. So, try using this example for your tests:

abstract class TestBase {
    @BeforeTest
    public void beforeTest() {
        // do config here 
        // this will run for each of you <test> tag in your testng.xml suite
    }

    @BeforeMethod
    public void beforeMethod() {
        // do some config here 
        // this will run for each method annotated with @Test
    }
}

class SomeTestClass extends TestBase {
    @Test
    public void some_test() {
        // some testing
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM