【问题标题】:Gherkin scenario outlines or multiple scenarios?小黄瓜场景大纲或多个场景?
【发布时间】:2019-12-15 00:20:18
【问题描述】:

在为 BDD 定义 Gherkin 格式的验收标准时,是否有首选方法?

我是否应该将场景拆分如下...

Scenario: User saves contact details
Given I am on the contact details page
When I enter the following details
| email          | phone     |
| pete@gmail.com | 012345678 |
And I save the details
Then the details are correctly saved

Scenario: User saves contact details with a very long email address
Given I am on the contact details page
When I enter the following details
| email                                                         | phone     |
| peterpeterperterlongemailaddress1234567890@gmailsomething.com | 012345678 |
And I save the details
Then the details are correctly saved

Scenario: User saves contact details with a very long phone number

etc

还是应该使用具有多个轮廓的单个场景?

Scenario: User saves contact details
Given I am on the contact details page
When I enter the following details
| email   | phone   |
| <email> | <phone> |
And I save the details
Then the details are correctly saved

Examples: 
| email                                                         | phone                 |
| pete@gmail.com                                                | 012345678             |
| peterpeterperterlongemailaddress1234567890@gmailsomething.com | 012345678             |
| pete@gmail.com                                                | 012345678901234567890 |

后者可能更容易适应/添加,但对我来说失去了场景的基本概念。

更新 我刚刚看到以下文章,其中讨论了这一点。建议后者是更好的方法。 https://www.hindsightsoftware.com/blog/scenario-outlines

它建议如下:

Scenario: User saves contact details
Given I am on the contact details page
When I enter the following details
| email   | phone   |
| <email> | <phone> |
And I save the details
Then the details are correctly saved

Examples: 
| scenario                  | email                                                         | phone                 |
| basic details saved       | pete@gmail.com                                                | 012345678             |
| very long email address   | peterpeterperterlongemailaddress1234567890@gmailsomething.com | 012345678             |
| very long phone number    | pete@gmail.com                                                | 012345678901234567890 |

【问题讨论】:

    标签: cucumber bdd specflow gherkin


    【解决方案1】:

    作为一般实践问题,如果您改变输入,但期望输出相同,那么您就有一个很好的场景大纲。

    您可以参数化Then 步骤,但我不建议这样做,因为更改测试的断言意味着您已经从根本上更改了测试。这是一个哲学和代码维护问题。

    考虑电话号码的最大长度,在您的示例中应该是 11 个字符。如果最多 11 个字符,并且您有一个测试该边界两侧的场景大纲,那么该场景有多种失败的原因。首先,当 11 不再是最大长度,此外,如果 12 现在是最大长度,该测试也将失败。

    有多种失败原因的测试是“不稳定的”

    下面的场景大纲会改变断言和输入,这意味着该测试有多种失败的原因。

    Scenario Outline: User saves contact details
        Given I am on the contact details page
        When I enter the following details
            | email          | phone   |
            | pete@gmail.com | <Phone> |
        And I save the details
        Then the details <Assertion> correctly saved
    
    Examples:
        | Phone                  | Assertion |
        | 012345678901234567890  | Are       |
        | 0123456789012345678901 | Are Not   |
    

    我什至建议将电子邮件场景与电话号码场景分开。

    每个测试都应该只有一个失败的原因。

    下面的两个场景似乎是彼此的重复,但保持大部分输入一致,并且只改变其中一个输入可以为您提供更稳定的测试,但由于显而易见的原因而失败:

    Scenario Outline: User saves contact phone number
        Given I am on the contact details page
        When I enter the following details
            | email          | phone   |
            | pete@gmail.com | <Phone> |
        And I save the details
        Then the details are correctly saved
    
    Examples:
        | Phone                 |
        | 012345678             |
        | 012345678901234567890 |
    
    Scenario Outline: User saves contact e-mail address
        Given I am on the contact details page
        When I enter the following details
            | email   | phone     |
            | <Email> | 012345678 |
        And I save the details
        Then the details are correctly saved
    
    Examples:
        | Email                                                         |
        | pete@gmail.com                                                |
        | peterpeterperterlongemailaddress1234567890@gmailsomething.com |
    

    现在,当场景失败时,场景名称会提示您可能应归咎于应用程序的哪一部分,这有助于调试。

    【讨论】:

    • 优秀的建议 Greg - 真的很有帮助。关于失败的多种原因的要点很明确 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多