Testing your tests is kind of crazy. However when writing a library of Cucumber step definitions which will be used in many projects it started to make sense to test my tests.
The step definitions are the code.
It helps reduce fear of breaking lots of projects which use the steps.
The tests/specs show examples of how to use the step definitions.
It is important to note that I’m not imply TDD/BDDing these step definitions. My use-case is adding tests afterwards when it comes time to extract them to a library.
How to test step definitions
Exercise the full step (with Rspec)
The common way of testing complex steps is to extract all the ruby from the step definitions and then just test that. But this way of testing does not exercise the step definitions from the outside, getting as close as possible to how they will be used. It also does not provide examples of how to use the step definitions.
So I sat down with Matt Wynne, who started this discussion and we thrashed out some Rspec macros for testing whole step definitions.
If we were testing this step definition (icalendar_steps.rb):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Our spec would look like this (Note: we test the Before hook as well as the step definition):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Experiment’s Source code
You can see the source on Github:
git clone git://github.com/mattwynne/cucumber-step_definitions.git
If this experiment proves successful these macros will make their way to a nice gem.