On Tue, 15 Jun 2004, Nathaniel Talbott wrote: > As has been mentioned here several times, offering patches or add-on > components is a middle ground between simply asking for functionality, > and re-implementing a completely new library almost identical to one > that is already available. More a hack than a patch, allows arbitrary ordering of tests: class TC_Zoo < Test::Unit::TestCase order :rand # randomly order :insert # insertion order order {|a,b| b <=> a} # reverse alphabetically order {|x| x.size} # by length of method names :) def test_neko puts "neko: nya" end def test_inu puts "inu: wan" end def test_tori puts "tori: pi" end def test_nezumi puts "nezumi: chu" end end --- test/unit/testcase.rb.orig Thu Nov 20 15:18:59 2003 +++ test/unit/testcase.rb Tue Jun 15 01:09:57 2004 @@ -38,14 +38,44 @@ @test_passed = true end + # Define the order in which tests are executed. + # :insert :insertion => insertion order + # :rand :random => random order + # 2-arg block => user-defined sort + # 1-arg block => user-defined sort_by + # default => alphabetic order + def self.order(*a, &block) + @order = block || a[0] + end + + # Capture insertion order of test methods. + def self.method_added(sym) + sym.to_s =~ /^test_/ or return + @insert ||= [] + @insert << sym.to_s + end + # Rolls up all of the test* methods in the fixture into # one suite, creating a new instance of the fixture for # each method. def self.suite - method_names = public_instance_methods(true) - tests = method_names.delete_if {|method_name| method_name !~ /^test./} + @insert ||= [] + tests = case @order + when :insert, :insertion + @insert + when :rand, :random + @insert.sort_by {rand} + when Proc + if @order.arity == 2 + @insert.sort {|a,b| @order[a,b]} + else + @insert.sort_by {|x| @order[x]} + end + else + @insert.sort + end suite = TestSuite.new(name) - tests.sort.each do + tests.each do |test| catch(:invalid_test) do suite << new(test) -- Relm