Steve
Steve

Reputation: 4701

Can I reuse a cucumber/gherkin Example block?

I've got two different scenarios that use the same example block. I need to run the example block for two different times of the day and I'm looking for a succinct way to do this (without copy+pasting my example block).

I'm replacing the yyymmdd with an actual date in my stepdef.

I'd like to reuse my Example block because in real life it's a MUCH longer list.

Scenario Outline: File arrives in the morning
Given a file <file> arrives in the morning
When our app runs
Then The file should be moved to <newFile>
And the date should be today
Examples:
|Filename|NewFilename|
|FileA|NewFileA_yyyymmdd|
|FileB|NewFileB_yyyymmdd|

Scenario Outline: File arrives in the evening
Given a file <file> arrives in the evening
When our app runs
Then The file should be moved to <newFile>
And the date should be tomorrow
Examples:
|Filename|NewFilename|
|FileA|NewFileA_yyyymmdd|
|FileB|NewFileB_yyyymmdd|

I'm implementing this in java, though I don't know if that's a relevant detail.

Upvotes: 0

Views: 396

Answers (2)

diabolist
diabolist

Reputation: 4109

This is something thats better tested at a lower level. What you are testing here is your file renaming algorithm. You could write a unit test to do this which would

  • run much much faster (100 1000 or even 10K times faster is perfectly realistic)
  • be much more expressive
  • deal with edge cases better

Once you have that done I would write a single scenario that deals with the whole end to end process, and just ensures that the file is moved and renamed e.g.

Given a file has arrived
When our app runs
Then the file should be moved 
And it should be renamed
And the new name should contain the current date

Cukes are expensive to create and particularly to run, so you need to get lots of functionality exercised for each one. When you use outlines and create identical scenarios you are just wasting loads of runtime and adding complexity for little benefit.

Upvotes: 0

Greg Burghardt
Greg Burghardt

Reputation: 18848

No, this is not supported in the Gherkin syntax. I don't often advised copy-and-paste, but this is one case where it is warranted due to a missing feature of the language.

Generally this should not be a big deal, as the example size should be small. It you really need a large number of examples then recreating this test in code only (Java, Python, C#, etc.) might be the best idea. Most unit test libraries offer some form of data driven tests that might provide a DRYer, more maintainable solution than Gherkin.

Upvotes: 3

Related Questions