我应该如何在场景大纲中命名我的步骤

时间:2012-01-12 20:27:00

标签: cucumber specflow gherkin

我有一些看起来像这样的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     |

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

2 个答案:

答案 0 :(得分: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会为您编写测试。

希望有所帮助!

答案 1 :(得分:0)

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

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   |

然后是另一种相反的情景。这使得场景意义非常清晰。如果您的常用步骤是通用的,您可以重复使用它们。我也试图来自用户的方法

相关问题