Order-Dependent Test Suites

The other day I was helping a customer with a persnickety failure in his MiniTest specs when I came across the following gem in MiniTest:
[code language=”ruby”]
##
# Call this at the top of your tests when you absolutely
# positively need to have ordered tests. In doing so, you’re
# admitting that you suck and your tests are weak.

def self.i_suck_and_my_tests_are_order_dependent!
class << self
undef_method :test_order if method_defined? :test_order
define_method :test_order do :alpha end
end
end
[/code]
As a test suite grows it is all too easy for what should be independent tests to inadvertently depend on one another. Frequently the root cause is state left behind in the database or a class definition or monkey patch visible at the top level. In an ideal world, we would give our test suites as much tender loving care as the rest of our code, but the reality is that order dependencies do happen. The goal should be to drive toward at least test scripts that commute if not individual test cases. In the meantime, a little public penance may be in order…

Post a Comment