Skip to content

Instantly share code, notes, and snippets.

@nicklewis
Created September 14, 2011 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicklewis/1217637 to your computer and use it in GitHub Desktop.
Save nicklewis/1217637 to your computer and use it in GitHub Desktop.
Binary search for order-dependent test failures
#!/usr/bin/env ruby
specs_in_order = File.read('spec_order').split
failing_spec = ARGV.first
specs = specs_in_order[0...specs_in_order.index(failing_spec)]
suspects = specs
while suspects.length > 1 do
count = suspects.length
# Try the first half
specs_to_run = suspects[0...(count/2)]
puts "Trying #{suspects.join(' ')}"
start = Time.now
system("rspec #{specs_to_run.join(' ')} #{failing_spec}")
puts "Finished in #{Time.now - start} seconds"
if $? == 0
puts "Innocent!"
# This group is innocent. The culprit is in the other half.
suspects = suspects[(count/2)..-1]
else
puts "Guilty!"
# One of these is guilty.
suspects = specs_to_run
end
end
puts "The culprit is #{suspects.first}"
@nicklewis
Copy link
Author

Create a file in the local directory called 'spec_order', with the list of specs in the order they were run to cause the failure. Pass as an argument to the program the path to the failing spec.

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