在小黄瓜中编写场景的最佳方式

Best way to write scenario in gherkins

刚开始使用小黄瓜,我需要为我的休息编写小黄瓜场景 API。

有一种情况,用户输入客户 ID 和订单 ID,其余服务根据一些检查将订单 ID 与客户 ID 链接起来。然后 returns 订单的完整详细信息。

我引用的小黄瓜场景如下:

Scenario: Associate an order to customer
    When User provides order to attach to customer
    Then User should get the associated order details

现在,如果客户或订单 ID 无效,其余调用将响应相应的错误消息。

我应该使用 GIVEN 来确保客户和订单 ID 存在吗?

GIVEN:客户退出 ID "abc" AND Order exit with Id "bcd"

这里GIVEN有什么意义吗? 编写此示例场景的最佳方式是什么?

是的,您应该提供一个 Given 来创建客户和订单。您实际上需要两个步骤,一个创建客户,另一个创建订单:

Scenario: Associate an order to customer
    Given a customer exists
    And an order exists
    When User provides order to attach to customer

实际上,我认为您在这里需要两个场景。一个断言客户和订单相互关联,另一个断言细节:

Scenario: Associate an order to customer
    Given a customer exists
    And an order exists
    When User provides order to attach to customer
    Then the customer should be associated with the order

Scenario: Retrieving the order details after associating a customer to an order
    Given a customer exists
    And the following order exists:
        | Field | Value |
        | A     | 1     |
        | B     | 2     |
        | C     | 3     |
    When User provides order to attach to customer
    Then User should receive the following order information:
        | Field | Value |
        | A     | 1     |
        | B     | 2     |
        | C     | 3     |

现在每个测试只有一个失败原因。如果订单未与客户关联,但无论如何 returns 订单信息,则与客户关联的场景将失败。断言订单详细信息的场景将继续通过。这有助于您调试测试失败。

另外,如果返回的订单详情的需求发生变化,但是关联客户与订单的需求没有变化,那么获取订单详情的场景将失败,而关联客户的场景随着命令继续传递。同样,这有助于缩小应用程序中的故障点。