views:

84

answers:

2

For example, when these tests are run, I want to ensure that test_fizz always runs first.

require 'test/unit'
class FooTest < Test::Unit::TestCase
    def test_fizz
        puts "Running fizz"
        assert true
    end

    def test_bar
        puts "Running bar"
        assert true
    end
end

Update: Why do I want to do this? My thought is that early failure by certain tests (those testing the simpler, more fundamental methods) will make it easier to track down problems in the system. For example, the success of bar hinges on fizz working correctly. If fizz is broken, I want to know that right off the bat, because there's no need to worry about bar, which will fail too, but with much more complicated output in the test results.

+2  A: 

Tests within the same test class are called in the order they are defined. However, test classes are run in alphabetical order by classname.

If you really need fine control, define the fizz and bar methods with a prefix other than test_ and from inside a test_fizz_bar method, call them in order and run bar conditionally upon success of running fizz.

EDIT: It seems like different unit test frameworks behave differently. For JUnit in Eclipse, it seems that the test cases run in random order: http://stackoverflow.com/questions/512778/ordering-unit-tests-in-eclipses-junit-view

Joy Dutta
Thanks for the suggestion. In the code I posted, the tests are **not** run in the order they are defined; rather, `test_bar` runs first.
FM
In my experience, tests within the same test class are invoked in alphabetical order.
Jonathan Julian
+1  A: 

Name the tests you want to run first with a low-sorting alphabetical name.

def test_AAA_fizz

For code readability, this could be considered ugly, or helpful, depending on your point of view.

Jonathan Julian