25

I've got a feature (a .feature file) that are working fine in cucumber.

The background of all the scenarios in the feature just sets up a user, and then logs in as a supervisor, e.g.

   Background:
     Given I am logged in as a supervisor with an existing supervisee

   ...loads of scenarios

However the design/goals of the application has changed and the same scenarios should all work whether you are logged in as a supervisor or as the user. This is not true for most of the rest of the application where the design is not symmetrical for supervisors/users.

Is there any sane way to avoid copying and pasting the whole of the feature file with a different background? It doesn't seem like there's a way to either parameterize background (e.g. with an Either: Or: stanza) or alternatively a way to pull in an external file with a load of scenarios. Ideas?

   Background:
     Given I am logged in as an existing supervisee

   ...same loads of scenarios

Here's some fantasy gherkin syntax (that doesn't exist)

   Background Outline:
     Given I am logged in as a <user>

   Backgrounds:
     | user                                   |
     | supervisor with an existing supervisee |
     | an existing supervisee                 |

   ...loads of scenarios

Alternatively different fantasy Gherkin syntax :

   Background:
     Given I am logged in as an existing supervisee

   Include Scenarios:
     supervisor.features
Tim Diggins
  • 4,364
  • 3
  • 30
  • 49
  • I'm not clear on what change you want to make. Can you show the background, explain what behavior it currently exhibits, and describe how you want it to behave? – Mark Thomas Oct 31 '12 at 01:08
  • @MarkThomas I've added an existing background, and the copy and paste version, and a couple of "fantasy gherkin syntax" to indicate what I'm aiming at – Tim Diggins Nov 02 '12 at 07:58
  • @TimDiggins I know this an old question, but I'm curious whether or not you found a way to describe "Background Outlines" using Gherkin? ...maybe someone came up with an extension? – muhqu Jan 29 '14 at 10:10
  • 1
    @muhqu, no, I just left the duplication in. I can see that too much complexity is probably against the grain of Cucumber. I've kind of fallen out of love with cucumber (as a developer, for most of the reasons that are at the heart of the Spinach project) and tend to just write features/acceptance tests in rspec these days. I still often write Gherkin-y (Given/When/Then) descriptions as a means of communications, but just don't make them executable. – Tim Diggins Jan 30 '14 at 10:27

1 Answers1

6

If it was me, I would just suck up the duplication:

http://dannorth.net/2008/06/30/let-your-examples-flow/

An alternative would be to use a tag on the feature that indicates you want to run the scenarios against both user groups. Then use an Around hook to run the scenario twice, once for each type of user.

We've talked about things like Background Outlines before, but the conclusion we came to was that it wouldn't be worth the extra complexity to implement it.

Matt Wynne
  • 136
  • 1
  • 1
  • I don't want the duplication, not because I'm a DRY zealot, but because it's really only one feature and more expressive to keep it in one place. But the tagging and using an around hook is very expressive. Perfect, thanks! – Tim Diggins Nov 09 '12 at 17:06
  • Does it really work? It seems that it is not possible to execute a block more than once. See [this](http://stackoverflow.com/a/10876638/158074) answer. – rsenna Mar 12 '13 at 22:53
  • @rsenna (apologies for late response) - I didn't actually try, but left the duplication in. I liked the idea of around hooks, but it was too much work (and it does look like it wouldn't have worked in the end). – Tim Diggins Jan 30 '14 at 10:29
  • 2
    I think the lack of background outlines makes cucumber extremely clunky, to the point where it is practically useless for all but the simplest cases. It's a no brainer to implement it IMHO. – David Talbot Jan 13 '15 at 20:09
  • 2
    I agree, this is a feature that should definitely be implemented. Anyone know if there has been any further discussion on this? – try5tan3 Aug 06 '15 at 15:38
  • just jumped right into my first job as a QA engineer and I just can't believe this hasn't been implemented - even after all those years. I've seen people on SO arguing that "having bg outlines would create an exponential amount of cases", but given the complexity of today's software, isn't this exactly what we need to properly cover as many scenarios as possible? I'm currently writing tests for and documenting a pretty simple software here and I already found that to cover every route possible I'd have to write an immense amount of unnecessarily redundant scenarios :S – arvere Aug 21 '18 at 13:48