Fixing brittle tests by running them again (shoulda)

Really fixing them would be nicer but that can be complicated, so just run them 3 times and if they fail every time fail, otherwise pass with warnings.

Code


  def self.brittle_should(test, &block)
    results = []
    tries = 3

    tries.times do |i|
      should test + " (try #{i+1})" do
        if results.empty? or results.none?
          begin
            instance_exec(&block)
            results << true
          rescue Object
            if results.size < tries
              warn "Brittle test #{test.inspect} failed, trying again..."
              results << false
            else
              raise
            end
          end
        end
      end
    end
  end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s