在Gherkin中分组步骤或连接场景

swa*_*nee 2 bdd scenarios cucumber gherkin behat

我正在使用Gherkin语言定义要在BDD工作流程中使用BehatCucumber等工具的功能.这是到目前为止的功能定义:

Feature: Save Resource
    In order to keep interesting resources that I want to view later
    As a user
    I need to be able to save new resources

    Scenario: Saving a new resource
        Given "http://google.com" is a valid link
        When I insert "http://google.com" as the new resource link
        And I insert "Google Search Engine" as the new resource title
        Then I should see a confirmation message with the inserted link and title
        When I accept
        Then I should have 1 additional resource with the inserted link and title

    Scenario: Aborting saving a new resource
        Given "http://google.com" is a valid link
        When I insert "http://google.com" as the new resource link
        And I insert anything as the new resource title
        Then I should see a confirmation message with the inserted link and title
        When I abort
        Then I should have the same number of resources as before

    Scenario: Saving a resource with invalid link
        Given "http://invalid.link" is an invalid link
        When I insert "http://invalid.link" as the new resource link
        And I insert anything as the new resource title
        Then I should see a confirmation message with the inserted link and title
        When I accept
        Then I should see an error message telling me that the inserted link is invalid

    Scenario: Saving a resource with already saved link
        Given "http://google.com" is an already saved link
        When I insert "http://google.com" as the new resource link
        And I insert anything as the new resource title
        Then I should see a confirmation message with the inserted link and title
        When I accept
        Then I should see an error message telling me that the inserted link already exists
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在几种情况下重复了很多样板.我知道我可以Background在所有场景之前定义一个要执行的步骤列表,我可以将所有步骤放到确认消息中,但如果我这样做,我将无法区分不同的链接和标题用户可能已插入.

是否可以定义仅用于某些场景而不是所有场景的背景?或者可能连接两个场景,例如要求某个场景(我可以重用)在另一场景之前运行?或者我应该继续重复样板?

Tho*_*erg 5

我会考虑考虑你的场景应该做什么.目前我看到的脚本讨论了如何完成工作.几乎没有什么应该做什么.

导航细节对于理解系统应该做什么并不是很有用.这是一个众所周知的初学者错误,如[1]中所述并在[2]中转录.

你想要做的是将UI细节推向堆栈.导航细节在您实现的步骤所使用的辅助方法/类中更好.页面对象是隐藏方案导航的一种方法.

当你摆脱导航细节时,你的一些复制将消失.出于可读性原因,剩余的重复可能是可接受的.

请记住,理解场景比重复复制要重要得多.

[1] https://cucumber.io/blog/2016/05/09/cucumber-antipatterns

[2] http://www.thinkcode.se/blog/2016/06/22/cucumber-antipatterns


dia*_*ist 5

继托马斯的回答之后

您的场景是复杂且重复的,因为它们每次都在描述用户与应用程序交互的“方式”。“如何”在场景中没有位置,因为

  1. 随着您对自己正在做的事情了解得更多,您做事方式的细节可能会发生变化。你不想每次改变你做某事的细节时都改变你的场景

  2. 在你的场景中放置如何使它们变得无聊、重复、难以阅读且实施起来成本高昂。

  3. 放置你的场景如何通常是避免做场景在这里的实际工作的一种方式,即找出“为什么”你在做某事,并优雅简洁地定义你想要实现的“什么”。

注意还有很多其他原因

让我们看看当你做这些额外的工作时你的场景变得多么简单

##老的

Scenario: Saving a new resource
    Given "http://google.com" is a valid link
    When I insert "http://google.com" as the new resource link
    And I insert "Google Search Engine" as the new resource title
    Then I should see a confirmation message with the inserted link and title
    When I accept
    Then I should have 1 additional resource with the inserted link and title
Run Code Online (Sandbox Code Playgroud)

##新的

Scenario: Bookmarking
  When I save a bookmark
  Then my bookmark should be saved
Run Code Online (Sandbox Code Playgroud)

##老的

Scenario: Saving a resource with invalid link
  Given "http://invalid.link" is an invalid link
  When I insert "http://invalid.link" as the new resource link
  And I insert anything as the new resource title
  Then I should see a confirmation message with the inserted link and title
  When I accept
  Then I should see an error message telling me that the inserted link is invalid
Run Code Online (Sandbox Code Playgroud)

##新的

Scenario: Bookmark with invalid link
  When I bookmark with an invalid link
  Then I should see an invalid link error
Run Code Online (Sandbox Code Playgroud)

等等。

请注意新场景如何如此清楚地说明您在增加业务价值方面所做的很少!!

至于你关于背景的问题,如果你需要两个不同的背景,你需要两个不同的 .feature 文件。这是好事!!