Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, how to I control the order in which Test::Unit tests are run?

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.

like image 741
FMc Avatar asked Nov 20 '09 19:11

FMc


People also ask

Which one is used to run the unit tests?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.

Are unit tests run in parallel?

Unit tests run one at a time. There is no parallelism.

How are unit tests run?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

How do I run a test case in Ruby?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.


3 Answers

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
.
like image 133
knut Avatar answered Nov 15 '22 19:11

knut


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

like image 38
Joy Dutta Avatar answered Nov 15 '22 18:11

Joy Dutta


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.

like image 26
Jonathan Julian Avatar answered Nov 15 '22 20:11

Jonathan Julian