cucumberbddbehatgherkinscenarios

Grouping steps or concatenating scenarios in Gherkin


I'm defining a feature to be used in a BDD workflow with tools such as Behat or Cucumber, using the Gherkin language. This is the feature definition so far:

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

As you can see, there's a lot of boilerplate repeated in several scenarios. I know I could define a Background with a list of steps to be executed before all scenarios, where I could put all the steps up to the confirmation message, but if I did that, I wouldn't be able to distinguish among different links and titles that the user might have inserted.

Is it possible to define a background that is used only for certain, and not all, scenarios? Or maybe to concatenate two scenarios, for example requiring that a certain scenario (that I can reuse) is run before another? Or should I simply keep repeating the boilerplate?


Solution

  • Following on from Thomas' answer

    Your scenarios are complicated and repetitive because each time they are describing 'how' the user interacts with the application. 'How' has no place in scenarios because

    1. The details of how you do things are likely to change as you learn more about what you are doing. You don't want to have to change your scenarios every time you change a detail of how you do something

    2. Putting how in you scenarios makes them boring, repetitive, difficult to read and expensive to implement.

    3. Putting how is your scenarios is generally a way of avoiding doing the real work that scenarios are here for, which is to find out 'why' your are doing something and define elegantly and succinclty 'what' you are trying to achieve.

    n.b. there are lots of other reasons

    Lets look at how simple your scenarios become when you do this extra bit of work

    ##Old

    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
    

    ##New

    Scenario: Bookmarking
      When I save a bookmark
      Then my bookmark should be saved
    

    ##Old

    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
    

    ##New

    Scenario: Bookmark with invalid link
      When I bookmark with an invalid link
      Then I should see an invalid link error
    

    etc.

    Note how the new scenarios illustrate so clearly just how little you are doing in terms of adding business value!!

    As for your question about backgrounds, if you need two different backgrounds you need two different .feature files. This is a good thing!!