JP Boily

Founder of Metrics Watch

Jack of all trade SaaS consultant (but mostly code, analytics & marketing)

January 11, 2013

RSpec: top failing specs report

January 11, 2013 - JP -  

You just made a big change that broke a lot of tests and you would love to know which specs have the most failing tests? Here is how to add a breakdown of the files with the most failing specs.

Create a spec/support/top_failing_specs.rb:

RSpec.configure do |config|
  failed_examples = {}
  config.after(:each) do |group|
    if group.example.instance_variable_get("@exception")
      failed_examples[group.example.metadata[:file_path]] ||= 0
      failed_examples[group.example.metadata[:file_path]] = failed_examples[group.example.metadata[:file_path]] + 1
    end
  end

  config.after(:suite) do
    if failed_examples != {}
      puts '------------------------------------------------'
      puts '              Top failing specs                 '
      puts '------------------------------------------------'
      failed_examples.sort_by {|k,v| v}.reverse.each do |k,v|
        puts "#{v.to_s.rjust(3)} | #{k}"
      end
      puts '------------------------------------------------'
    end
  end
end

Example of output:

------------------------------------------------
              Top failing specs
------------------------------------------------
 18 | ./spec/models/my_model_spec.rb
  4 | ./spec/models/your_failing_model_spec.rb

Please note that if you have a shit load of failing tests, that will work better with some custom RSpec formatter like Fuubar.

Go fix your tests now!