@BeforeTest
我对诸如or之类的注释有疑问@BeforeMethod
。是否可以设置全局注释,以便我所有的测试类都将使用它们?我的框架中有超过 20 个测试类和很多测试方法。每个测试类都有类似@BeforeTest
or的先决条件@BeforeMethod
,但是对于每个测试类,这个先决条件是相同的。所以我认为这可能是一个好主意,编写一个通用的注释方法,可以在每个测试类中使用。
问问题
54 次
2 回答
5
使用继承使代码可重用。创建一个超类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
于 2021-09-14T21:16:16.453 回答
0
使用 and 的实现ITestListener
,IClassListener
您可以执行以下操作。onTestStart
将在每个测试用例之前调用,并将在您的套件中调用,并onStart
为每个类执行一次。<test>
onBeforeClass
public class MyListener implements ITestListener, IClassListener {
@Override
public void onStart(ITestContext context) {
// Put the code in before test.
beforeTestLogic();
}
@Override
public void onBeforeClass(ITestClass testClass) {
// Put the code in before class.
beforeClassLogic();
}
@Override
public void onTestStart(ITestResult result) {
// Put the code in before method.
beforeMethodLogic();
}
}
现在将@Listener
注释添加到所需的测试类:
@Test
@Listener(MyListener.class)
public class MyTest {
// ........
}
于 2021-09-15T13:26:56.043 回答