Re: [Cucumber] scenario independence?

242 views
Skip to first unread message

Rob Park

unread,
Jul 11, 2012, 4:30:30 PM7/11/12
to cu...@googlegroups.com


On Wed, Jul 11, 2012 at 1:37 PM, Matthew Fremont <mfre...@springpartners.com> wrote:
As a newcomer to Cucumber, I'm trying to understand whether scenarios within the same feature should be written and implemented with the expectation that scenarios are executed independently.

Yes, scenarios should be independent.
 
Specifically, I want to understand whether to expect that data created/modified by one scenario is visible to another scenario or each scenario starts with no lingering data from a prior scenario.

It is true that data left behind by a previously run scenario will still be present, but this is also true if you just run the same scenario twice in a row. You can either check to see if data exists before creating it (not explicitly in the scenario, but preferably in code below some step) or you can clean up after yourself.

For example, in the first actual project feature I'm trying to implement with Cucumber I have several overlapping "Given" clauses:

Scenario: like a comment
  Given the user X
  When user X likes a page
  ...

Scenario: create a comment
  Given the user X
  When user X creates a comment
  ...

To me, this might read better as:
  Given user "X" creates a comment
  When ...
 
From what I've been able to determine, a feature with scenarios like my example will result in the "Given the user X" step being executed twice.

Should I be writing the step def for the Given in a way that assumes that the user may have been created by the step def being executed for a prior scenario?

Probably. 

Similarly, should the When and Then step defs be written to presume that they could be executing in a context where the data may have been modified by another scenario in the same feature?

No. Your Givens should ensure that the state of the app is how you want it. 

Are scenarios always executed in the order in which they appear in the feature file?

From my newcomer's perspective, I'd think that it would be desirable to write and implement scenarios which assume independence, since that removes any dependency on execution order and make the outcome of the specification deterministic, but I'm open to consider other points of view.


Thanks,

Matthew

//rob

Rahul Sharma

unread,
Jul 11, 2012, 4:35:03 PM7/11/12
to cu...@googlegroups.com

On 11 Jul 2012, at 20:37, Matthew Fremont wrote:

As a newcomer to Cucumber, I'm trying to understand whether scenarios within the same feature should be written and implemented with the expectation that scenarios are executed independently. Specifically, I want to understand whether to expect that data created/modified by one scenario is visible to another scenario or each scenario starts with no lingering data from a prior scenario.

For example, in the first actual project feature I'm trying to implement with Cucumber I have several overlapping "Given" clauses:

Scenario: like a comment
  Given the user X
  When user X likes a page
  ...

Scenario: create a comment
  Given the user X
  When user X creates a comment
  ...

From what I've been able to determine, a feature with scenarios like my example will result in the "Given the user X" step being executed twice.

Should I be writing the step def for the Given in a way that assumes that the user may have been created by the step def being executed for a prior scenario?

Hi Matthew,

I ran into a similar issue a couple of days ago and you'd find my post with title "Before Feature hook" which I thought would solve this problem of having to run one particular step once per feature but I have been advised that it is generally considered a bad idea. So I guess for now the scenarios will have to be independent of each other and the Given step will run for each scenario. For more details please look into that post.

Similarly, should the When and Then step defs be written to presume that they could be executing in a context where the data may have been modified by another scenario in the same feature?

Are scenarios always executed in the order in which they appear in the feature file?

From my newcomer's perspective, I'd think that it would be desirable to write and implement scenarios which assume independence, since that removes any dependency on execution order and make the outcome of the specification deterministic, but I'm open to consider other points of view.


Thanks,

Matthew






-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en

Regards,
Rahul Sharma

Stephen Abrams

unread,
Jul 11, 2012, 4:35:39 PM7/11/12
to cu...@googlegroups.com
On Wed, Jul 11, 2012 at 3:37 PM, Matthew Fremont
<mfre...@springpartners.com> wrote:
> As a newcomer to Cucumber, I'm trying to understand whether scenarios within
> the same feature should be written and implemented with the expectation that
> scenarios are executed independently. Specifically, I want to understand
> whether to expect that data created/modified by one scenario is visible to
> another scenario or each scenario starts with no lingering data from a prior
> scenario.

I had this question a while back, and have generally come to the
decision that a good practice is to let each scenario express and
populate its starting state. I don't know best practices for clean up,
but I sometimes clean up my scenarios by adding annotations that will
execute clean-up code. For example, if a scenario creates directories,
and I add a @creates_directories annotation to it, I would have an
AfterStep hook tied to the annotation that would delete any new
directories. This is nice because it doesn't pollute the scenario
description.

Here is a pretty in depth thread from my asking of this question:
https://groups.google.com/forum/#!msg/cukes/RpVRa3-cBfM/ljnhKQo_nMwJ
>
> For example, in the first actual project feature I'm trying to implement
> with Cucumber I have several overlapping "Given" clauses:
>
> Scenario: like a comment
> Given the user X
> When user X likes a page
> ...
>
> Scenario: create a comment
> Given the user X
> When user X creates a comment
> ...
>
> From what I've been able to determine, a feature with scenarios like my
> example will result in the "Given the user X" step being executed twice.

While this is still executed per scenario, you may find "Scenario
Background", another Cucumber feature to be useful.
>
> Should I be writing the step def for the Given in a way that assumes that
> the user may have been created by the step def being executed for a prior
> scenario?
>
> Similarly, should the When and Then step defs be written to presume that
> they could be executing in a context where the data may have been modified
> by another scenario in the same feature?
>
> Are scenarios always executed in the order in which they appear in the
> feature file?
>
> From my newcomer's perspective, I'd think that it would be desirable to
> write and implement scenarios which assume independence, since that removes
> any dependency on execution order and make the outcome of the specification
> deterministic, but I'm open to consider other points of view.
>
>
> Thanks,
>
> Matthew
>
>
>
>
>
> -- Rules --
>
> 1) Please prefix the subject with [Ruby], [JVM] or [JS].
> 2) Please use interleaved answers
> http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
> 3) If you have a question, don't reply to an existing message. Start a new
> topic instead.
>
> You received this message because you are subscribed to the Google Groups
> Cukes group. To post to this group, send email to cu...@googlegroups.com. To
> unsubscribe from this group, send email to
> cukes+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/cukes?hl=en



--
Steve
Reply all
Reply to author
Forward
0 new messages