Java 黄瓜 jvm 的全局 BeforeAll 钩子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19770103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global BeforeAll Hook for cucumber-jvm?
提问by Christian
The ruby version of cucumber supports a global before hook. An *.rb file placed in the features/support directory gets apparently called only once, before any and all scenarios run. See https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks
ruby 版本的cucumber 支持全局before 钩子。放置在 features/support 目录中的 *.rb 文件显然只会在任何场景运行之前调用一次。见https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks
That seems to be a great way to make sure a database (used in read-only during tests) gets populated (and thus is in a known state) before any tests run.
这似乎是确保数据库(在测试期间以只读方式使用)在任何测试运行之前填充(因此处于已知状态)的好方法。
Is there a similar feature available for the Java version of cucumber?
Java 版本的黄瓜是否有类似的功能?
回答by Blacklight
As far as I know global hooks are not supported by Cucumber-JVM. However, you could try (tagged) hooks, @Before annotations and as a work-around static fields. Have a look herefor an example.
据我所知,Cucumber-JVM 不支持全局钩子。但是,您可以尝试(标记)钩子、@Before 注释和作为解决静态字段的方法。看看这里的一个例子。
回答by nat
Please follow this link. this a workaround for @BeforeAll and @AfterAll
请点击此链接。这是@BeforeAll 和@AfterAll 的解决方法
回答by Ashley Frieze
I have solved this problem a little bit using a class rule. Let's say that we wanted our cucumber test to start a single TestContainers
container that we were testing. Let's say we were testing REDIS (we're not but it's a simple example).
我已经使用类规则稍微解决了这个问题。假设我们希望我们的黄瓜测试启动TestContainers
我们正在测试的单个容器。假设我们正在测试 REDIS(我们不是,但这是一个简单的例子)。
@RunWith(Cucumber.class)
@CucumberOptions(...)
public class TestRunner {
@ClassRule
static GenericContainer REDIS = new GenericContainer<>("redis:5.0.3-alpine")
.withExposedPorts(6379);
// obviously we weren't testing redis, but this gives you the idea of a container
}
The above causes the TestContainers
class GenericContainer
to be initialized before the cucumber lifecycle and then torn down afterwards. You can write your own custom JUnit rule, extending TestRule
and use it to decorate the execution of tests with your own custom set up.
以上导致TestContainers
类GenericContainer
在黄瓜生命周期之前被初始化,然后在之后被拆除。您可以编写自己的自定义 JUnit 规则,TestRule
使用您自己的自定义设置扩展和使用它来装饰测试的执行。
A common problem with this is that you have to somehow get access to the objects created at this point in the lifecycle from things created on a per-scenario basis. However, given that this lifecycle is part of the static state of the Cucumber test suite, you can just access the test rule object via the static field of the suite.
一个常见的问题是,您必须以某种方式从基于每个场景创建的事物访问生命周期中此时创建的对象。但是,鉴于此生命周期是 Cucumber 测试套件静态状态的一部分,您只需通过套件的静态字段访问测试规则对象即可。
I have a blog post on this here - https://codingcraftsman.wordpress.com/2020/01/20/extending-the-cucumber-test-lifecycle/
我在这里有一篇博文 - https://codingcraftsman.wordpress.com/2020/01/20/extending-the-cucumber-test-lifecycle/
回答by JFK
There is no such feature natively in Cucumber JVM (see https://github.com/cucumber/cucumber-jvm/issues/515).
Cucumber JVM 本身没有这样的功能(参见https://github.com/cucumber/cucumber-jvm/issues/515)。
However, there are a couple of workarounds:
但是,有几种解决方法:
- Use the hooks of your test framework:
@BeforeAll
and@AfterAll
for JUnit 5,@BeforeClass
and@AfterClass
for JUnit 4
- Use a Cucumber
Before
hook for lazy singleton initialization and a JVM shutdown hook for teardown - Implement a Cucumber
EventListener
and subscribe toTestRunStarted
andTestRunFinished
events - Use the integration test lifecycle features of your build framework, e.g. Maven's
pre-integration-test
,integration-test
,post-integration-test
phases and themaven-failsafe-plugin
.
- 使用测试框架的钩子:
@BeforeAll
和@AfterAll
用于JUnit的5,@BeforeClass
和@AfterClass
JUnit 4
- 使用 Cucumber
Before
挂钩进行懒惰的单例初始化,使用 JVM 关闭挂钩进行拆卸 - 实现一个 Cucumber
EventListener
并订阅TestRunStarted
和TestRunFinished
事件 - 使用构建框架的集成测试生命周期功能,例如 Maven 的
pre-integration-test
、integration-test
、post-integration-test
阶段和maven-failsafe-plugin
.
You will also have to solve the issue of injecting the results of such a setup step (e.g. random port numbers) into your tests.
您还必须解决将此类设置步骤的结果(例如随机端口号)注入测试的问题。
I wrote a blog article to cover all details: https://metamorphant.de/blog/posts/2020-03-10-beforeall-afterall-cucumber-jvm-junit/
我写了一篇博客文章来涵盖所有细节:https: //metamorphant.de/blog/posts/2020-03-10-beforeall-afterall-cucumber-jvm-junit/