After (My) RSpec best practices and tips, I’m happy to share my Cucumber best practices and tips!
This article will help you organize, clarify and reduce the size of your cucumber scenarios.
1. Organize your garden
Keep your feature files organized by grouping them by business object, then action and context if any. I put all the feature files in the same directory. For instance:
1 2 3 4 5 |
|
The steps specific to the application should be organized by business object as well (bank_account_steps.rb
, user_steps.rb
…). Keep the file organized grouping the steps by Given
/ When
/ Then
.
Do not overload the files generated by Cucumber like step_definitions/web_steps.rb
and support/env.rb
with your own steps, helpers or setup code. These files are likely to get overwritten when you update Cucumber so store your stuff in your own files.
2. Custom steps make your scenario DRY and accessible
Scenarios should have the same lifecyle as your code: Red, Green, Refactor to make them DRY and easy to read.
Group multiple steps together. For instance:
1 2 3 4 5 6 |
|
… could be refactored to:
1
|
|
This step definition is the following:
1 2 3 4 5 6 7 8 |
|
This step can then be easily reused in other scenario keeping your features DRY. It also decouples the scenario from the UI so that you won’t have to change dozens of feature files when the UX guru changes translations or user flows.
3. Background: setup the DRY way
Make the feature focus on one business object/action/context and the background will get longer than the scenarios.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
4. Scenario outlines: scenario with variables!
A scenario outline contains variables allowing you to test multiple context using a truth table. For instance I use them to make sure that validation errors are displayed properly:
1 2 3 4 5 6 7 8 9 10 11 |
|
5. Multi-line step arguments: give your step a table for lunch!
A step can take a multi-line table as an argument. This is a great way to load up a bunch of data or to test the rendering of lists and tables. For instance:
1 2 3 4 5 6 |
|
The step definition looks like the following:
1 2 3 4 5 |
|
I hope that these tips will help you growing healthy cucumber features!
Want more? Check out the Cucumber wiki, 15 Expert Tips for Using Cucumber by Dave Astels, You’re Cuking It Wrong by Elabs and You’re almost cuking it… by Antony Marcano. You could also like (My) RSpec best practices and tips. :)
Happy BDD!
Thanks to Jean-Michel Garnier for reviewing this article.