Cucumber - 场景之间是否可以共享一张表? [英] Cucumber - Is it possible to share a table between Scenarios?
问题描述
有谁知道它可以在 .feature 文件中定义一个表 &然后从多个场景中访问它?我没有链接场景,但他们中的许多人确实需要将具有相同数据的表传递给他们的步骤定义——同样由于这个原因,示例在这里并不能真正满足我的需要.
Does anyone know its its possible to define a table once in a .feature file & then access it from multiple scenarios? I'm not chaining scenarios but many of them do need to pass tables with the same data to their step definitions - also for this reason examples won't really do what I need here.
谢谢!
推荐答案
一种可能的解决方案是在需要表格的地方标记所有场景:
One possible solution is to tag all your scenarious where you need a table:
@given_have_table
Scenario: test
Then I am happy
然后将 Before
钩子绑定到此标记并调用从 Before
钩子定义中声明您的表的步骤:
Then bind Before
hook to this tag and call step that declares your table from within Before
hook definition:
Before("@given_have_table") do
steps Q%{
Given I have the following table:
| a | b |
| 1 | 2 |
}
end
另一种方法是在标记的Before
钩子中构造所需的表而不调用步骤:
Alternative approach is to construct required table in tagged Before
hook without calling step:
Before("@given_have_table") do
@tbl = { :a => 1, :b => 2 }
end
这篇关于Cucumber - 场景之间是否可以共享一张表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!