Skip to content

Instantly share code, notes, and snippets.

@natritmeyer
Created April 5, 2011 20:00
Show Gist options
  • Save natritmeyer/904406 to your computer and use it in GitHub Desktop.
Save natritmeyer/904406 to your computer and use it in GitHub Desktop.
Cucumber formatter for printing unused steps
# Copyright (c) 2010, Nathaniel Ritmeyer. All rights reserved.
#
# http://www.natontesting.com
#
# Save this in a file called 'unused.rb' in your 'features/support' directory. Then, to list
# all the unused steps in your project, run the following command:
#
# cucumber -d -f Cucumber::Formatter::Unused
#
# or...
#
# cucumber -d -f Unused
require 'cucumber/formatter/stepdefs'
class Unused < Cucumber::Formatter::Stepdefs
def print_summary(features)
add_unused_stepdefs
keys = @stepdef_to_match.keys.sort {|a,b| a.regexp_source <=> b.regexp_source}
puts "The following steps are unused...\n---------"
keys.each do |stepdef_key|
if @stepdef_to_match[stepdef_key].none?
puts "#{stepdef_key.regexp_source}\n#{stepdef_key.file_colon_line}\n---"
end
end
end
end
@leonardoanalista
Copy link

uninitialized constant Cucumber::Formatter::Stepdefs (NameError)

@natritmeyer
Copy link
Author

What version of cucumber have you got? I wrote this against 0.10.2 (latest at time of writing)...

@leonardoanalista
Copy link

My bundle install:
...
Using cucumber (0.10.2)
...

I run:
bundle exec cucumber features/my_feature.feature -d -f Cucumber::Formatter::Unused

result:
no such file to load -- cucumber/formatter/unused
Error creating formatter: Cucumber::Formatter::Unused (LoadError)
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/constantize.rb:16:in require' /usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/constantize.rb:16:inconstantize'
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/configuration.rb:75:in formatter_class' /usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/configuration.rb:167:informatters'
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/configuration.rb:163:in map' /usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/configuration.rb:163:informatters'
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/configuration.rb:68:in build_tree_walker' /usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/runtime.rb:42:inrun!'
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/main.rb:43:in execute!' /usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/../lib/cucumber/cli/main.rb:20:inexecute'
/usr/lib/ruby/gems/1.8/gems/cucumber-0.10.2/bin/cucumber:14
/usr/bin/cucumber:19:in `load'
/usr/bin/cucumber:19

Unused.rb location:

ls -l features/support/unused.rb
-rw-r--r-- 1 leonardo leonardo 748 2011-04-06 19:58 features/support/unused.rb

thanks mate

@natritmeyer
Copy link
Author

Which one is it? The 2 errors you mention (comment #1 and #3) aren't the same...

@leonardoanalista
Copy link

This is the way worked:
cucumber -d -f Unused

not: cucumber -d -f Cucumber::Formatter::Unused

@natritmeyer
Copy link
Author

Yeah, that works too!

@jamesottaway
Copy link

Just upgraded to Cucumber v2.4.0 and needed to tweak what is above:

require 'cucumber/formatter/stepdefs'

class Unused < Cucumber::Formatter::Stepdefs
  def print_summary
    add_unused_stepdefs
    aggregate_info

    @stepdef_to_match.keys
      .sort_by { |stepdef| [stepdef.location.file, -stepdef.location.lines.first] }
      .select { |stepdef| @stepdef_to_match[stepdef].none? }
      .each do |stepdef|
        $stdout.puts "#{stepdef.location.file}:#{stepdef.location.lines.first} # #{stepdef.regexp_source}"
      end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment