在编写BDD功能时,我应该将之前的用户交互放入Given步骤还是When步骤?

时间:2012-02-27 20:49:38

标签: bdd gherkin

我正在尝试编写多步骤业务流程的需求(通过向导)。我有许多场景,无论您是否被允许在另一个屏幕上选择选项,用户与一个屏幕的交互都会发生变化。

例如(我掩盖了实际业务,但步骤的过程和形式几乎完全相同):

Feature: Personal Diagnostic Search Filter
  In order to select a Technician who offers Personal Diagnostics,
    when I've asked for a Personal Diagnostic
  As a Business Customer
  I want to limit my search to Technicians who offer Personal Diagnostics

  Background:
    Given a Business named "Big Al's Auto Supply"
      And a Customer named "Bob Test" at the "Big Al's Auto Supply" Business
      And an Account named "bobtest@testbusiness.com" owned by "Bob Test"
      And the "bobtest@testbusiness.com" Account has the "Repair Order Creator"
        permission
      And you log in as "bobtest@testbusiness.com"
      And you start scheduling a new Repair Order

  Scenario: Enter the Select Technician page when Use PD is selected
    Given you select Use PD
    When you enter the Select Technician page
    Then the PD Filter should be visible
      And the PD Filter should be selected

  Scenario: Basic Search for Technicians when PD Filter is selected
    Given a Technician named "PD Technician"
      And the Technician named "PD Technician" supports PD
      And a Technician named "Non-PD Technician"
      And the Technician named "Non-PD Technician" does not support PD
      And you select Use PD
      And you enter the Select Technician page
      And you select the PD Filter
      And you select Basic Search
    When you search for Technicians with the name "Technician"
    Then your search results should contain "PD Technician"
      And your search results should not contain "Non-PD Technician"

但在the Gherkin wiki上,建议您:

  

避免谈论用户中的用户互动

他们继续作出例外,但是:

  

登录用户(无交互建议的例外。“先前发生的事情”是可以的。)

该页面上也有说:

  

步骤的目的是描述用户执行的关键操作

什么属于一个什么属于什么属于什么如果你有很多UI互动?

在我的第一个场景中,select Use PD需要UI交互,因为它是创建新修订订单的向导的一部分。但是,这是PD过滤器可见的前提条件,并在用户进入“选择技术人员”页面时触发。

第一种情况可能不是那么糟糕,但第二种情况会加剧问题。点击搜索时会触发搜索,但必须执行大量的UI互动才能导航到该页面。其中一些互动也无法掩盖,因为必须选择Use PD才能使搜索过滤器出现。但这些UI互动不是该场景的关键操作

1 个答案:

答案 0 :(得分:9)

作为一般规则,尽可能多地尝试说明方案,就像您正在进行对话一样,并尽可能多地排除无关信息。

例如,我会喜欢上面的场景,阅读:

Given our customer Bob Test is scheduling a repair order
And we have two technicians: "Fred Technician" and "George Nontechnician"
When Bob Test decides he wants a Personal Diagnostic
And he selects a technician
Then the search results should only contain "Fred Technician"

然后做任何必要的事情来使这些步骤起作用 - 无论是登录还是其他方式。请注意,我没有谈到“页面”或采取实际步骤 - 它们应该对用户来说是直观的。 BDD不是关于测试。它是关于人们将如何使用系统的示例,以便您可以围绕这些示例进行对话并探索它们,查找异常和不同的场景等。

检查过滤器是否可见并不重要。用户不关心过滤器是否可见。他关心他可以使用过滤器来获得他的结果,所以就这样做。

在代码中,我通常在我的步骤之间传递一个“World”对象。这可以让很多gubbins走开。我没有使用Gherkin,但我想它提供了类似的能力。您可以在那里存储所有用户详细信息,您创建了哪些技术人员,以便您可以检查它是否不会在结果中带回“George Nontechnician”等。

使用角色的友好名称也很有用,因为人们可以想象Fred和George的样子。

摆脱任何不会对场景产生影响的事情,并不会帮助人们想象它会发生什么。你知道Bob有权安排订单,因为这就是他正在做的事情 - 只需在该步骤中添加必要的东西。

“何时”是您有兴趣描述的行为。在这种情况下,您对过滤Personal Diagnostics的能力感兴趣,因此与行为相关的所有用户交互都应该在“When”中,之前的所有交互都应该在“Givens”中。我发现尝试思考结果不同的背景是有用的 - 例如,如果没有可用的PD技术人员会发生什么?这告诉我有什么不同;我们将设置不同的上下文,但执行相同的事件。上下文在Given中,事件在When中。 (在介绍“背景”之前,这曾经简单得多)。

一般情况下,如果你在观察场景时眼睛茫然,那你就做错了。

希望这有帮助。

相关问题