在单个小黄瓜场景中有多组Given / When / Then是否可以

时间:2013-10-15 22:56:54

标签: testing bdd gherkin

我正在Gherkin编写验收测试,我想根据初始操作测试Web应用程序UI中的多个更改。这是一个例子:

        Scenario: Cancel editing a new text asset
            Given the user "test_user@fakedomain.com" is logged in
            When the user navigates to "/build/"
            And the user clicks the "Sandbox" link
            And the user inputs "Test story for canceling editing of a new text asset" for the "title" field
            And the user inputs "Test User" for the "byline" field 
            And the user inputs "My summary, so exciting!" for the "summary" textarea
            And the user clicks on "Untitled Section" in the section list
            And the user clicks the "Text" icon in the "center" container 
            And the user inputs the following text in the rich text editor:
                    """
                    Test text for asset. This is cool. 
                    """
            And the user clicks the "cancel" button
            Then the following text is not present: 
                    """
                    Test text for asset. This is cool. 
                    """
            And the "Image" icon is present
            And the "Text" icon is present
            When the user refreshes the browser 
            And the user clicks on "Untitled Section" in the section list
            Then the following text is not present:
                    """
                    Test text for asset. This is cool. 
                    """
            When the user opens the asset drawer
            Then the following text is not present:
                    """
                    Test text for asset. This is cool.
                    """

请注意,有多组When / Then步骤,用于测试初始操作的响应。虽然大多数步骤的实现都忽略了prefix关键字,而且我很确定我可以运行这个测试,有没有更好的方法来测试不同的结果?使用相同的设置但不同的“Then”语句编写多个场景是否更好?

1 个答案:

答案 0 :(得分:26)

请记住,您应该一次只测试一个行为/功能。 经验法则是你应该只使用一步

Given some state before action
  And some other state before action
  ...
When  only one action
Then  assert correct output
  And assert correct output
  ...

你看 - 只有一行When,在When下没有任何Ands。如果您使用许多步骤而不是步骤,则创建测试脚本,而不是规范。您的测试很难理解,您会发现在底层实现发生变化时会添加越来越多的步骤。

您还需要隐藏底层逻辑,因为您不希望每次更改不相关的内容时都更改它。例如:

  

用户输入“我的摘要,太刺激了!”为“摘要”   textarea的

如果将摘要字段从textarea更改为输入类型,该怎么办?你必须改变场景(维护噩梦)或让你处于场景中(比没有场景更糟糕)。你应该写:

When the user describes it as "so exciting!"

但是,整个情景的结构仍然很糟糕。问自己一个问题:我想检查什么?如果我是一个想要了解该功能的业务逻辑的人,我希望看到类似的内容:

Scenario: Cancel editing a new text asset
  Given I edit the Sandbox details with some data
  When  I cancel editing
  Then  Sandox details should be empty

就是这样!

如何实现呢?更深入地移动所有不相关的逻辑,使用PageObject pattern等。并阅读Specification By Example

相关问题