Gherkin 场景是否总是需要 When 步骤?

14

在Gherkin中定义场景时,有时候Given和When步骤之间没有明确的区别,即用户没有与系统进行主动交互,验证的目的是验证系统在某些情况下应该呈现什么样子。

请考虑以下内容:

Scenario: Show current balance
Given user is on account page
Then user should see his balance

对抗

Scenario: Show current balance
When user goes to account page
Then user should see his balance

我不确定我是否总是会使用第二种变体。如果我有多个场景共享上下文“用户在账户页面”,其中一些具有其他用户操作,而另一些则没有,则在我的看法中,即使某些场景缺少“When”步骤,也应该将“用户在账户页面”保留为一个Given步骤。这个方法有效吗?


这只涉及到可读性问题,没有所谓的“应该”。场景不需要有“When”。 - Andrei Botalov
4个回答

19

Cucumber/SpecFlow在正式和技术上并不要求您编写一个When步骤,或者说Given/When/Then只需按照场景中编写的顺序执行即可。在这方面,您不需要一个When步骤。

但是,正如Andy Waite所写,When步骤显示系统从“设置”到达新状态时采取的操作或事件,该状态在Then步骤中进行验证。在这方面,每个测试都应该有一个When步骤(正如您所写的:否则我们在测试什么)。

这就留下了您最后的评论;那么如何验证设置(例如给定系统启动,则数据库干净)。在这种情况下,可以跳过When步骤。

因此,一如既往,重点在于可读性和理解。场景编写的目的是使我们对系统行为的想法具体和明确。使用最适合理解和学习相关行为的形式。

没有过多思考,我可能会猜测一般建议始终使用使事件或行为非常明显和清晰的When步骤。尽可能避免隐含和隐藏行为。

希望这可以帮助您。


8

通常情况下,场景由三个部分组成:

  • 设置(即给定条件)
  • 操作(即何时进行操作)
  • 验证(即结果应该如何表现)

有时候设置部分并不是必要的(或者是隐含的)。但是我想不出任何需要操作和验证部分都不需要的情况。


1
完全同意验证(否则为什么要测试?)但在某些情况下仍不确定操作。如果我们只需要验证设置是否正确,该怎么办? - Vagif Abilov
你可能在编写测试时这样做,但我认为你不会保留它。 - Andy Waite
你在这个例子中看到了什么操作?(链接为 https://stackoverflow.com/questions/47892611/how-to-test-filters-based-on-authorization-using-gherkin) - komarik
@komarik,请查看我对那个问题的回答:https://stackoverflow.com/a/47945176/10608 - Alexander Bird

5

我同意Andy和Marcus的观点,但我有一些评论可能会有用。

  1. Gherkin feature files should act as living documentation for the behaviour of your system. For this reason scenarios should provide enough detail to convey to a developer and other project stakeholders (product owner, testers etc) the business rules that embody that feature.

    I think your question may have arisen from not considering this business rule end to end when articulating the scenario. I'd have to ask someone the question , what is a balance? Therefore I feel you may need a step to at least convey the concept - that before a user can look at their balance, they have to have one.

    Scenario: Show current balance
      Given I have a balance
       When I go to my account page
       Then I should see my balance
    
  2. It's important to set system state (i.e. any 'Given' step) to allow you to clearly test that the system is working correctly - otherwise how are you going to determine that the balance is actually correct? You may wish to make this more explicit by specifying some arguments:

    Scenario: Show current balance
      Given my balance is £10
       When I go to my account page
       Then I should see my balance as £10
    
  3. I'm not sure which BDD framework you are using but I use Behat which allows you to map more than one Gherkin step to a step definition. I.e

    user is on account page
    user goes to account page
    

    can both map to a step definition which navigates a user to a page. The system behaviour is the same, the only reason to distinguish between the two, as you have, would be to make your scenarios more readable.


0
据我理解,编写场景需要以下三个步骤:
  1. 应用程序在开始时应处于的状态。
  2. 用户必须执行的操作以达到某个状态。
  3. 用户操作的结果/输入,即您场景的终点。
因此,场景将类似于:
Given the user is on the profile page
When the user goes to the balance page
Then the user should see their balance

个人资料页面将是用户可以单击按钮或链接访问其余额的地方。

然后有一个背景:

Given the user is logged in
And the user has a balance

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接