【问题标题】:How should I name my steps in a scenario outline我应该如何在场景大纲中命名我的步骤
【发布时间】:2012-02-22 06:25:31
【问题描述】:

我有一些看起来像这样的 specflow 测试:

Scenario: Person is new and needs an email
Given a person
And the person does not exist in the repository
When I run the new user batch job
Then the person should be sent an email

Scenario: Person is not new and needs an email
Given a person
And the person does exist in the repository
When I run the new user batch job
Then the person should not be sent an email

除了 2 个场景之外,我有 10 个非常相似的场景,都具有步骤类型,所以我想使用“场景大纲”。不幸的是,我很难想出一种可读的方式来重写我的步骤。

目前,我想出了这个,但看起来很笨重:

Scenario: Email batch job is run
Given a person
And the person '<personDoes/NotExist>' exist in the repository
When I run the new user batch job
Then the person '<personShould/NotGetEmail>' be sent an email

Examples:
| !notes   | personDoes/NotExist | personShould/NotGetEmail |
| Exists   | does not            | should                   |
| No Exist | does                | should not               |

我也考虑过这一点,虽然它更干净,但几乎没有传达意义

Scenario: Email batch job is run
Given a person
And the person does exist in the repository (is '<personExist>')
When I run the new user batch job
Then the person should be sent an email (is '<sendEmail>')

Examples:
| !notes   | personExist | sendEmail |
| Exists   | false       | true      |
| No Exist | does        | false     |

有没有人有更好的方法来参数化诸如“做”、“不做”、“应该”、“不应该”、“有”、“没有”之类的概念?在这一点上,我正在考虑将所有内容保留为不同的场景,因为它更具可读性。

【问题讨论】:

  • 你目前有 10 个测试...这似乎并不多,除非测试本身有很多步骤 - 如果它们确实有很多繁琐的步骤,我们也许可以按摩测试成形,以便表格和场景大纲变得多余。您能否发布一个实际测试作为示例(最复杂的测试?),并强调变体测试中发生变化的事情?

标签: cucumber specflow gherkin


【解决方案1】:

这是我过去所做的:

Given these people exist in the external system
| Id | First Name | Last Name | Email |
| 1  | John       | Galt      | x     |
| 2  | Howard     | Roark     | y     |
And the following people exist in the account repository
| Id | External Id | First Name | Last Name |
| 45 | 1           | John       | Galt      |
When I run the new user batch job
Then the following people should exist in the account repository
| External Id | First Name | Last Name | Email |
| 1           | John       | Galt      | x     |
| 2           | Howard     | Roark     | y     |
And the following accounts should have been sent an email
| External Id | Email |
| 2           | y     |

您可以使用 SpecFlow 中的 table.CreateSet() 和 table.CreateSet() 辅助方法将表格快速转换为假外部系统存储库和数据库中帐户表的数据。

然后您可以使用 table.CompareToSet(accountRepository.GetAccounts() 将“Then”子句中的表与数据库中的记录进行比较。

巧妙的是,您编写的所有步骤都可以在多种情况下重复使用。您只需更改表中的数据,SpecFlow 就会为您编写测试。

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    也许你应该把它们分成两个场景

    Scenario Outline: User exists in the repository
    Given a person
    | Field | Value   |
    | First | <first> |
    | Last  | <last>  |
    And the person exists in the repository
    When the user attempts to register
    Then the person should be sent an email
    
    Examples:
    | first   | last   |
    | Bob     | Smith  |
    | Sarah   | Jane   |
    

    然后是相反的另一种情况。这使场景含义非常清晰。如果您的常用步骤是通用的,您可以重复使用它们。我也尝试从用户的角度来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2016-02-13
      • 2017-03-28
      • 2016-07-13
      相关资源
      最近更新 更多