【发布时间】:2013-12-08 00:38:55
【问题描述】:
在使用 SpecFlow 熟悉行为驱动开发后,我想知道是否有多个场景用于相同的功能,如下所示:
Register.feature
Feature: Register a new user
In order to use the system,
one must register with the system
so that one gets authorized and may login
Scenario: Register a new user using valid credentials
Given I am on the registration page
When I have entered my desired username "UserName" and password "password"
And I have confirmed my password "password"
And I click the register button
Then I shall get confirmation that I am now a registered user
除了我的场景可能有点过于繁琐之外,还必须设法验证注册过程中的其他场景,例如:
- 输入的用户名太短
- 输入密码太短
- 输入密码不包含数字
- 输入密码与确认密码不符
仅举几例。我已阅读有关使用 SpecFlow 功能文件的标签的信息,因此我可能可以执行以下操作:
@shorterPasswordProvided
Scenario: Register a user using a password that is too short
Given I am on the registration page
When I have entered my desired user name
And I have provided a password that is too short "allo"
And I click the Register button
Then I shall get an error message which mentions about the password minimum length
@noCredentialsAtAll
Scenario: Register a user using no credentials at all
Given I am on the registration page
When I click on the Register button with no credentials entered
Then I shall get an error message that says I have to fill all required fields in
然后,使用[BeforeScenario("myTag")] 应该可以解决问题。
挂钩允许按照特定规则执行测试的子集。因此,When 方法可以在预定义的上下文中执行,即要执行它的钩子,并通过 BeforeScenario 或类似属性提及。
我是否理解正确,还是我在这里迷茫?
我是不是太过分了?
我错过了什么吗?
是否所有“密码太短”、“未提供凭据”都考虑了不同的使用场景,还是它们只能适合代码中的其他地方,比如单元测试本身?
我的意思是,所有这些场景都属于Register特性,因此它们应该在同一个Register.feature SpecFlow特性文件中定义,对吧?
【问题讨论】:
-
BeforeScenario 步骤在场景执行之前运行,所以当您说“使用 [BeforeScenario("myTag")] 应该可以解决问题”时,我不明白您的意思。您能否为您尝试使用 BeforeScenario 实现的目标添加更多细节?
-
其实在我目前所做的所有搜索中,我能找到的都与标签/钩子有关:github.com/techtalk/SpecFlow/wiki/Hooks
标签: tags bdd specflow scenarios feature-file