我对像 @BeforeTest
或 @BeforeMethod
这样的注解有疑问。是否可以设置全局注释,以便我所有的测试类都将使用它们?我的框架中有 20 多个测试类,其中包含大量测试方法。每个测试类都有像 @BeforeTest
或 @BeforeMethod
这样的先决条件,但对于每个测试类来说,这个先决条件是相同的。所以我认为这可能是个好主意,编写一个通用的注释方法,可以在每个测试类中使用。
最佳答案
使用继承使代码可重用。创建父类(super class)DefaultTestCase
:
public class DefaultTestCase{
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("beforeMethod");
}
}
并且每个测试用例类都扩展了 DefaultTestCase
:
public class ATest extends DefaultTestCase{
@Test
public void test() {
System.out.println("test");
}
@Test
public void anotherTest() {
System.out.println("anotherTest");
}
}
输出:
beforeTest
beforeMethod
test
beforeMethod
anotherTest
关于java - 不同测试类中使用的通用注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69183553/