8

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.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
FMc
  • 41,963
  • 13
  • 79
  • 132
  • Why do you want to do this? I certainly hope the order of your tests does not affect the test results. If it does, you're doing something terribly wrong. – gotgenes Nov 20 '09 at 20:03
  • I agree with gotgenes. I see your update, but if bar relies on fizz, you should test fizz, then stub the results of fizz when testing bar to avoid cross-contamination rather than ensuring your tests run in a particular order. If fizz is testing higher level functionality, maybe you should make it a functional test instead. – Kyle Nov 20 '09 at 23:06
  • 1
    Having a suite of ordered assertions are fine, but it's not unit testing anymore. Its Functional/Integration/Behavioral (whatever you want to call it....its all just the same). You should do with a tool designed for it like rspec – ryber Nov 21 '09 at 13:19
  • 1
    Related: http://stackoverflow.com/questions/1376267/ruby-executing-tests-in-a-random-order-with-rake – Andrew Grimm Apr 30 '10 at 03:12

3 Answers3

7

You can define the test order with Test::Unit::TestCase#test_order = :defined

Example:

gem 'test-unit'  #I used 2.5.5
require 'test/unit'
class Mytest < Test::Unit::TestCase
  self.test_order = :defined
  #~ self.test_order = :random
  #~ self.test_order = :alphabetic #default
  def test_b
    p :b
  end
  def test_a
    p :a
  end
  def test_c
    p :c
  end
end

The result:

Loaded suite test
Started
:b
.:a
.:c
.

Finished in 0.001 seconds.

Without test_order = :defined you get the alphabetic order:

Loaded suite test
Started
:a
.:b
.:c
.
knut
  • 27,320
  • 6
  • 84
  • 112
5

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: Ordering unit tests in Eclipse's JUnit view

Community
  • 1
  • 1
Joy Dutta
  • 3,416
  • 1
  • 19
  • 19
3

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
  • 12,163
  • 2
  • 42
  • 48