Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing different backgrounds #339

Closed
ghost opened this issue May 23, 2015 · 2 comments
Closed

Reusing different backgrounds #339

ghost opened this issue May 23, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented May 23, 2015

I am currently learning cucumber and I was checking some examples here: https://github.com/webjam/webjam/tree/master/features

Most of the scenarios contains

Given I am not logged in

or

Given I am logged in

so this is a clearly reusable part of the features.

According to the documentation it should be moved to a Background e.g. by https://github.com/webjam/webjam/blob/master/features/home.feature

Feature: Viewing the home page

  In order to see all the cool stuff
  I should be able to visit the home page

  Background:
    Given I am not logged in

  Scenario: not logged in
    When I view the home page
    Then I see the page

  Scenario: viewing / on a mobile
    When I view the home page from an iphone
    Then I am redirected to the mobile page

  Scenario: viewing / on a mobile with redirect-to-mobile=no
    When I view the home page from an iphone specifying redirect-to-mobile=no
    Then I am not redirected to the mobile page
    And I see the page

  Scenario: viewing the mobile version with an upcoming event, a past event and a post
    Given there is an upcoming event
      And there is a past event
      And there is a post
    When I view the mobile version of the home page
    Then I see the page

  Scenario: viewing the mobile version without any events or posts
    When I view the mobile version of the home page
    Then I see the page

The problem that sometimes not every scenario have the same background in a feature. So some of them need a logged in state, while some of the need a not logged in state. Afaik. there is no support to reuse such code. Probably split into multiple features, but I thought it would be better to use groups:

Feature: Viewing post pages

  In order to absorb Lachlan's deep insights into man and machine
  I should be able to visit pages and pages and pages of blog posts

  Group:
    Background:
      Given I am logged in

    Scenario: viewing a post when logged in
      When I view the post page
      Then I see the page

    Scenario: viewing a mobile post when logged in
      When I view the mobile post page
      Then I see the page

    Scenario: viewing a mobile post when not logged in
      When I view the mobile post page
      Then I see the page


  Group:
    Background:
      Given I am not logged in

    Scenario: viewing a post when not logged in
      When I view the post page
      Then I see the page

By some of the cases this hierarchical grouping won't work, so this is not a perfect solution. Tagging would be perfect, but I am unsure whether I should use it in this cases. Any opinion?

Another possible solution to match the "when not logged in" part from the scenario text using a helper that adds the Given I am not logged in. So the code would be such simple:

Feature: Viewing post pages

  In order to absorb Lachlan's deep insights into man and machine
  I should be able to visit pages and pages and pages of blog posts

    Scenario: viewing a post when logged in
      When I view the post page
      Then I see the page

    Scenario: viewing a mobile post when logged in
      When I view the mobile post page
      Then I see the page

    Scenario: viewing a mobile post when not logged in
      When I view the mobile post page
      Then I see the page

    Scenario: viewing a post when not logged in
      When I view the post page
      Then I see the page

I am not sure whether this is possible by the current test runner.

@ghost ghost changed the title Grouping scenarios by background (feature request) Reusing different backgrounds May 23, 2015
@jbpros
Copy link
Member

jbpros commented Jun 5, 2015

This should be discussed on cucumber/gherkin3 instead.

I haven't put much thought in it but the Group: keyword is interesting and deserves some thoughts. Matching the scenario name seem too error-prone to me (but you can achieve that in a before hook in your own suite).

@Inf3rno Can you reopen this on the gherkin3 repo please?

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant