1

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

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

4

2 回答 2

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 会为您编写测试。

希望有帮助!

于 2012-01-12T20:41:56.153 回答
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   |

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

于 2012-02-21T22:25:31.180 回答