【问题标题】:Multiple scenarios within a specification feature file?规范功能文件中的多个场景?
【发布时间】: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


【解决方案1】:

好的,你有几个问题,所以我会解决它们:

然后,使用 [BeforeScenario("myTag")] 应该可以解决问题。

BeforeScenario 钩子属性用于在场景执行之前运行一些代码。它通常用于为场景设置环境(例如,用相关数据填充测试数据库);如果用于这个目的,那么AfterScenario的使用也可以用来清理BeforeScenario的结果。

挂钩允许执行测试的子集 按照一定的规则执行。所以,一个When方法可以是 使用预定义的上下文执行

如果我对您的理解正确,您希望能够使用标签来控制场景中的步骤何时可以运行/不运行。这对于 SpecFlow 的钩子属性是不可能的;有一个 BeforeStep 钩子,但这只能让您在步骤运行之前执行代码,它不允许忽略该步骤。

是否考虑了所有“密码太短”、“未提供凭据” 不同的使用场景,还是它们只能 适合代码中的其他地方,比如单元测试本身?

在您的示例中,是的,这些是您的“注册新用户”功能的不同场景。如果您在开发中采用严格的 BDD 方法,那么通过“由外而内”的开发方法,您还将实施单元测试(通过作为 BDD 过程的一部分回退到 TDD),这也将涵盖“密码太短”和“未提供凭据”验证。

至于你的场景:

When  I have entered my desired username "UserName" and password "password"

不要使用这个,而是使用:

When I enter my  username "UserName" 
And I enter my password "password"

通过这样做,您将能够在“使用太短的密码注册用户”中重新使用“当我输入密码时”。这使我进入:

And I have provided a password that is too short "allo"

没有必要有一个单独的步骤来说明密码太短。重复使用:

When I enter my password "allo"

出于同样的原因,请勿使用:

When I click on the Register button with no credentials entered

只是重复使用:

When I click on the Register button

【讨论】:

  • +1 简洁的解释,新鲜!谢谢!这可能是我的 Gherkin 代码语法高亮试图让我理解不高亮某些 Given 和 When 的原因。我将重构以查看您的建议带来的变化。
  • @WillMarcouiller 很高兴我能帮上忙 :) 如果你想让我详细说明我的答案,请告诉我。
  • 您的回答帮助我发现场景中已实现部分的突出显示方式(白色)与未实现部分(紫色)不同。所以,我认为是语法错误,因为文本没有以紫色突出显示,实际上不是因为我试图将两个场景一个接一个地放置,而是因为我的场景步骤定义文件。此外,通过您的回答中建议的重用,我简化了步骤定义代码和测试,就像 Clean Code 强烈建议的那样。谢谢新鲜! =)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多