How to list all cucumber step definitions

If you’re using cucumber you probably have step definitions split across several files, and you’re probably using a tool that doesn’t do a good job of listing the available step definitions (one of the biggest downsides of using cucumber).

What follows is a script that will list them all. It’s fairly rudimentary in that for each step definition it just lists the regex, any regex modifiers and the step definition arguments. It does everything I need at the moment so I haven’t developed it any further. Do with it what you will.


root_test_folder = "../my_project/features/support"

Dir.glob(File.join(root_test_folder,'**/*.rb')).each do |support_file|
  File.new(support_file).read.each_line do |line|
    next unless line =~ /^\s*(?:Given|When|Then)\s+\//
    matches = /(?:Given|When|Then)\s*\/(.*)\/([imxo]*)\s*do\s*(?:$|\|(.*)\|)/.match(line).captures
    matches[0] = Regexp.new(matches[0])
    puts matches.inspect
  end
end

Set the root_test_folder variable to the relevant location, run the file, and your console will be filled with what amounts to a step definition dictionary!

This entry was posted in Automated Testing, Example Code, Ruby, cucumber and tagged , , . Bookmark the permalink.

4 Responses to How to list all cucumber step definitions

  1. Better:

    cucumber –dry-run –format usage features

  2. Nat says:

    Hi Aslak, there’s an even better version here:

    http://www.natontesting.com/2010/01/11/updated-script-to-list-all-cucumber-step-definitions/

    …which mentions using the -f stepdefs formatter. What I wanted was to create a ‘dictionary’ of step definitions to give to people who write tests. The usage formatter does that but since it also lists where the steps are used in feature files it adds noise to the output I wanted.

    What I ended up using is:
    cucumber -d -f stepdefs

    …because it lists only available step definitions, not where they are used.

  3. Jorge says:

    I’m using pickle and I have been unable to use –dry-run with it

  4. Nat says:

    What… with “-f stepdefs”? With my script? (Yeah, that won’t work) With cucumber?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>