我在我的集成测试中使用 cucumber-jvm
,我需要在所有场景完成后执行一些代码,只需执行一次。
仔细阅读了一些帖子,例如 this并审查了这份报告issue ,我已经完成了它做这样的事情:
public class ContextSteps {
private static boolean initialized = false;
@cucumber.api.java.Before
public void setUp() throws Exception {
if (!initialized) {
// Init context. Run just once before first scenario starts
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// End context. Run just once after all scenarios are finished
}
});
initialized = true;
}
}
}
我认为以这种方式完成的上下文初始化(相当于BeforeAll
)是可以的。然而,尽管它有效,但我完全不确定使用 Runtime.getRuntime().addShutdownHook()
的 AfterAll
模拟是否是一个好的做法。
所以,这些是我的问题:
- 我是否应该避免
Runtime.getRuntime().addShutdownHook()
来实现AfterAll
? - 是否有其他更好的选择来模拟 cucumber-jvm 中的
AfterAll
行为?
在此先感谢您的帮助。
最佳答案
可能更好的方法是使用构建工具,例如 Ant、Maven 或 Gradle 来进行设置和拆卸操作,这是集成测试的一部分。
使用 Maven Fail Safe Plug-in 时,用于设置集成测试。阶段 pre-integration-test
,通常用于设置数据库和启动 web 容器。然后运行集成测试(阶段 integration-test
)。然后运行 post-integration-test
阶段,用于关闭和关闭/删除/清理东西。
INFO如果 Cucumber 测试是通过 JUnit 运行的,以下内容也可能值得考虑
如果设置更简单、更小,您可以查看 JUnit @BeforeClass 和 @AfterClass。或者实现一个 JUnit @ClassRule,它有自己的 before()
和 after()
方法。
@ClassRule
public static ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
myServer.connect();
}
@Override
protected void after() {
myServer.disconnect();
}
};
关于java - AfterAll 全局钩子(Hook) cucumber-jvm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31563261/