java - 不同测试类中使用的常用注解
问题描述
@BeforeTest
我对诸如or之类的注释有疑问@BeforeMethod
。是否可以设置全局注释,以便我所有的测试类都将使用它们?我的框架中有超过 20 个测试类和很多测试方法。每个测试类都有类似@BeforeTest
or的先决条件@BeforeMethod
,但是对于每个测试类,这个先决条件是相同的。所以我认为这可能是一个好主意,编写一个通用的注释方法,可以在每个测试类中使用。
解决方案
使用继承使代码可重用。创建一个超类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
推荐阅读
- javascript - 以有效的方式将大型 HTML 转换为 Excel (xlxs) 文件
- c# - 查看距剖切面一定距离的剖面
- docker - 运行 npm install 时 Docker 没有这样的文件 package.json
- javascript - 页面加载之间的 javascript worker
- mysql - 如何将列名打印为mysql中的行数?
- c - 将我的 yacc 和 lex 文件放在一起时,yacc 无法识别我的令牌。为什么?
- mysql - 尝试解决 MYSQL 更新触发器的语法错误
- python - 如何在kivy中固定一条线的长度
- python - 如何将数组转换为结构化数组的特殊项并将其还原?
- javascript - 用户在Java中输入密码错误3次后如何让我的Java代码终止