【问题标题】:Best way to write scenario in gherkins在小黄瓜中编写场景的最佳方法
【发布时间】:2019-10-23 01:38:39
【问题描述】:

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

有这样一种场景,用户输入客户 ID 和订单 ID,其余服务根据一些检查将订单 ID 与客户 ID 联系起来。然后返回完整的订单详情。

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

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”的客户出口 以及 ID 为“bcd”的订单退出

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

【问题讨论】:

    标签: testing integration-testing gherkin


    【解决方案1】:

    是的,您应该提供创建客户和订单的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     |
    

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

    另外,如果返回的订单明细需求发生变化,但关联客户与订单的需求不变,则获取订单明细的场景会失败,而关联客户与订单的场景会失败。订单继续通过。同样,这有助于缩小应用程序的故障点。

    【讨论】:

    • 感谢您的详细回答,添加 Given 语句是有意义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多