Quiet Backtrace

Quiet Backtrace suppresses the noise in your Test::Unit backtrace. It also provides hooks for you to add additional silencers and filters.

Install

sudo gem install quietbacktrace

Usage

Quiet Backtrace works by adding new attributes to Test::Unit::TestCase. By default, their values are:

self.quiet_backtrace = true
self.backtrace_silencers = [:test_unit, :gem_root, :e1]
self.backtrace_filters = [:method_name]

Silencers remove lines from the backtrace that return true for given conditions. Filters modify the output of backtrace lines.

Override the defaults by adding your own backtrace_silencers:

class Test::Unit::TestCase
  self.new_backtrace_silencer :shoulda do |line| 
    line.include? 'vendor/plugins/shoulda'
  end
  self.backtrace_silencers << :shoulda
end

Or your own backtrace_filters:

class Test::Unit::TestCase
  self.new_backtrace_filter :ruby_path do |line|
     ruby_file_path = '/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8'
     line.slice!(0..(line =~ ruby_file_path)) if (line =~ ruby_file_path)
  end
  self.backtrace_filters << :ruby_path
end

Turn Quiet Backtrace off anywhere in your test suite by setting the flag to false:

Test::Unit::TestCase.quiet_backtrace = false

Rails-specific usage

After you have installed the gem:

cd rails-app-folder/vendor/gems
gem unpack quietbacktrace

Open rails-app-folder/config/environments/test.rb and add:

require 'quietbacktrace'

Quiet Backtrace comes with an excellent Rails-specific silencer and filter. They must be added (usually in test_helper.rb) because they are not turned on by default:

class Test::Unit::TestCase
  self.backtrace_silencers << :rails_vendor
  self.backtrace_filters   << :rails_root
end

Because Quiet Backtrace works by adding attributes onto Test::Unit::TestCase, you can add and remove silencers and filters at any level in your test suite, down to the individual test.

Resources

Authors

Special thanks to Boston.rb for cultivating this idea at our inaugural hackfest.

Requirements

  • Test::Unit
  • aliasing.rb and attribute_accessors.rb, which are snipped from ActiveSupport and are included in the gem. They allow quiet_backtrace.rb to use alias_method_chain, cattr_accessor, and mattr_accessor.

Copyright

Copyright© 2007 Dan Croak, thoughtbot, inc., released under the MIT license