首页 文章

参数化黄瓜查询中的示例

提问于
浏览 1983
1

我是Cucumber的新手 . 我要求在特征文件示例中使用变量而不是实际值 . 实际值将填充在单独的属性文件中 .
示例功能文件:

@tag
    Feature: Add an element to stack
      The user pushes an element. It gets added to stack
      @tag1
      Scenario: Push element to empty stack
        Given Stack is empty
        When User pushes an element
        Then stack should have only one element

      @tag2
      Scenario Outline: Push element to stack
        Given Stack has {initial} elements
        When User adds {new} element
        Then Length of stack increases to {new_size}
        | initial       | new       | new_size       |
        |           1   |       2   |               2|
        |           5   |       9   |               6|
        |           0   |       3   |               1|

输出示例应该是:

| initial       | new       | new_size       |
    |   {val1_1}    |{val1_2}   |        {val1_3}|
    |   {val2_1}    |{val2_2}   |        {val2_3}|

我使用“{}”而不是“<>”,因为我无法在预格式化的代码中打印<>内的元素

1 回答

  • 1

    使用场景大纲和示例 . 它将解决您基于数据输入的查询 . 例如,

    @tag2
     Scenario Outline: Push element to stack
      Given Stack has <initial> elements
      When User adds <new> element
      Then Length of stack increases to <new_size>
      Examples:
      | initial | new | new_size |
      | 1       | 2   | 2        |
      | 5       | 9   | 6        |
      | 0       | 3   | 1        |
    

    你的步骤定义是这样的,

    Given("^Stack has (.*) elements$", (String initial) -> {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        });
    

    类似地,对于其他查询参数,例如(name,new_size) .

相关问题