2016-11-10 62 views
0

我有一个testNG的小问题。下面我的示例代码:TestNG如何在beforetest继承失败时跳过测试

abstract class parent { 

abstract void beforeTest(); 

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

class child extends parent { 

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

而问题是如何做到这一点代码正常工作?所以我想执行beforeTest()方法,如果测试方法失败,应该跳过。我怎样才能做到这一点?

+0

是不是它像你想要的那样工作? – talex

+0

不,因为即使beforeTest失败,测试方法也会执行。 – user3552976

回答

0

通常,配置方法进入父类,测试类应该扩展父类。 因此,请尝试使用此示例进行测试:

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 
    } 
} 
+0

是的,我现在认为这应该是正确的解决方案,但是项目规范要求childs类实现配置方法。这就是为什么我在父类的beforeTest声明之前使用abstract关键字。 – user3552976

+0

然后你的解决方案是正确的。 – Vlad

+0

你应该在你的testng套件中运行子类。另外,在重写方法时总是使用@Override注解(重构代码时它会有很大帮助) – Vlad

相关问题