【问题标题】:Is there a "for each" or equivalent syntax for Gherkin/Cucumber?Gherkin/Cucumber 是否有“for each”或等效语法?
【发布时间】:2015-04-01 19:10:24
【问题描述】:

对于 Gherkin 是否有任何等效的“for each”语句?在以下场景中,我正在测试的页面有多个日期字段,我想在这些日期字段上运行相同的测试示例。

这是我想要建模的场景。

场景大纲:修改日期控件的精度值

Given I have just added a record

 When I select <precision>

  And I select <value>

 Then <date> displays in the <date type> field

例子:

  | date type | precision | value           | date                     |

  | Date 1    | Unknown   | N/A             | "Unknown"                | 

  | Date 1    | Year      | <current year>  | <current year>           |

  | Date 1    | Month     | <current month> | <current month, year>    |

  | Date 1    | Day       | <current day>   | <current month/day/year> | 

  | Date 2    | Unknown   | N/A             | "Unknown"                | 

  | Date 2    | Year      | <current year>  | <current year>           | 

  | Date 2    | Month     | <current month> | <current month, year>    | 

  | Date 2    | Day       | <current day>   | <current month/day/year> | 

假设同一页面上有 5 个日期类型字段。似乎没有必要在表中再复制/粘贴 12 行来覆盖日期 3 - 日期 5。这就是为什么我想知道是否有一个“for each”等价物,这样我就可以对每个日期类型执行相同的示例,而无需必须在示例表中明确显示。或者也许我可以用不同的方式来构建场景?

感谢您提供的任何帮助!

【问题讨论】:

    标签: cucumber gherkin


    【解决方案1】:

    不,cucumber 在设计时并没有考虑到循环。 Cucumber 的范围是允许从用户的角度定义应用程序的期望。而且由于真正的用户不会“循环”,因此在 cucumber 中实现它是没有意义的。

    从编程上讲,可以做的是编写一个程序,为应用程序中的每个组合生成相同的黄瓜脚本。

    【讨论】:

      【解决方案2】:

      Cucumber 不是为支持多列迭代而设计的,但可以让它工作。在这里,我想尝试一下路径和角色的每种组合:

        Scenario: cannot access paths
          When I access "path" as "role" then I should see an error
            | /path1 | user1 |
            | /path2 | user2 |
            | /path3 | user3 |
      
      When(/^I access "path" as "role" then I should see an error$/) do |table|
        paths, roles = table.raw.transpose.map { |e| e.reject(&:blank?) }
        roles.each do |role|
          step "I am logged in as #{role}"
          paths.each do |path|
            p "#{role} user visiting #{path}"
            visit path
            step 'I should see the privileges error'
          end
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2011-01-26
        • 1970-01-01
        • 2017-04-15
        • 2012-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        相关资源
        最近更新 更多