Cucumber:如何在多个场景下只执行一次后台
我有一个带有背景部分的功能,需要花费几分钟;它通过 API 在远程系统上设置状态。我希望它只对随后的所有场景执行一次,而不是对每个场景都执行一次。我的场景不会改变任何状态,它们是只读的,因此从一种场景到另一种场景不应该有副作用。
我没有使用 Rails,也没有本地数据库,因此无法处理数据库事务。
我目前正在考虑在 env.rb 中对 Before 做一些事情,并有一些基于标签的隐式约定/含义的条件代码,例如“@background-only-once”,可以使一些自定义代码跳过这些步骤,或者它可以在步骤定义中执行不同的逻辑——但是尝试在执行之间共享它有点令人讨厌。
想法?
I have a feature with a background section which takes multiple minutes; it's setting up state on a remote system via an API. I'd like to have it execute only once for all the scenarios which follow, instead of executing for every scenario. My scenarios don't change any state, they're read-only so there should be no side-effects from one scenario to another.
I'm not using rails, and don't have a local database, so can't do stuff with database transactions.
I'm currently thinking to do something with Before in env.rb and have some conditional code based on an implicit convention/meaning for tags, e.g. "@background-only-once", could make some custom code skip the steps, or it could execute different logic in the step-definitions – but it's a bit nasty to try to share this across executions.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不能真正解决问题,但这就是我在这种情况下所做的。
我会选择一种场景,然后在评论中标记不同的“场景”。如果您只是注释掉额外的场景标题,这正是会发生的情况。
这是注释掉第二个场景并在第一个功能结束时运行测试状态的示例
This doesn't really solve it, but here's what I do in that situation.
I go to one scenario, and just label the different "scenarios" in comments. It's exactly what would happen if you just comment the additional scenario headers out.
Here's an example of commenting out the second scenario, and running of the state of the test at the end of the first feature
如果你不希望你的后台针对每个场景都被执行,那么它就不是后台了,不是吗?
如果您每次运行 Cucumber 场景时都调用远程服务,不要这样做。相反,请按照 http://marnen.github.io/webmock- 中所述使用 Webmock 和 VCR演示文稿/webmock.html。您的测试将更快、更准确。
If you don't want your background to be executed for each scenario, then it's not a background, is it?
If you're calling remote services each time you run your Cucumber scenario, don't do that. Instead, use Webmock and VCR as described at http://marnen.github.io/webmock-presentation/webmock.html. Your tests will be faster and more accurate.