I've been working with Cucumber for about a year and have been continually refactoring the features and step definitions along the way. I have tons of steps defined across many files and I can't help but feel like many of them are no longer needed. Is there a way to find which cucumber step definitions are no longer being used?
Asked
Active
Viewed 7,074 times
3 Answers
45
The stepdefs formatter can do this, e.g.:
cucumber --dry-run -f stepdefs
It will print 'NOT MATCHED BY ANY STEPS' for any non-matches.
If you have any steps that are only used by other steps, then omit --dry-run
to get accurate results. With --dry-run
, the steps are not executed and cucumber will not find out that the referred step is actually used.

Wayne Conrad
- 103,207
- 26
- 155
- 191

Andy Waite
- 10,785
- 4
- 33
- 47
-
Sounds like what I want, but when I run that I get `Using the default profile... uninitialized constant Object::Factory (NameError)` – Peter Brown Mar 03 '12 at 22:35
-
Please post the full stack trace – Andy Waite Mar 05 '12 at 16:35
-
2I found this in the cucumber book last night and apparently it doesn't load the env.rb file which explains why things are failing. I was able to get it to run after moving things around and it's exactly what I want. Thanks! – Peter Brown Mar 05 '12 at 17:36
-
1I think I ended up using `-f usage` instead of `-f stepdefs` though. – Peter Brown Mar 05 '12 at 17:39
-
If you have "@wip" features this will report any steps used in those as unused. does anyone know how to include "@wip" features in this build? – amleszk Mar 26 '12 at 00:45
-
6Can you elaborate on what things needed to be moved around? I get the same uninitialized constant error, but with capybara. – Hana Sep 24 '13 at 14:25
-
2To fix errors @PeterBrown alluded to, I added `require_relative "env"` to the top of the first file alphabetically in `features/support/` (I'm guessing cucumber loads the support files alphabetically for some reason) – Nathan Wallace Mar 26 '18 at 18:52
-
Thanks @NathanWallace. The reason for this is the `--dry-run` flag, which omits loading env.rb (as stated in the cucumber `--help`). – codener Nov 28 '18 at 14:00
-
Hi guys, Is there a way to detect all steps in feature files that do not have any step definition methods? Like a check that returns list of all step sentences from feature files? – Abhijeet Vaikar Apr 17 '19 at 11:21
5
You could try running your stories under a coverage tool such as simplecov.
By default it will probably exclude test code such as cucumber steps since normally one is interested in how much of the app code is covered rather than how much of the test code, but that should be easy to reverse.

Frederick Cheung
- 83,189
- 8
- 152
- 174
0
For me
cucumber-js --dry-run --format usage
did the job.
Output print is something like
│ some feature test name │ UNUSED │ src\step_definitions\team-edit-steps.ts:24│

Tristate
- 1,498
- 2
- 18
- 38