4

There is the question In Ruby, how to I control the order in which Test::Unit tests are run? and wanted to answer with a reference to test_order = :defined,

The documentation for Test::Unit::TestCase.test_order says:

Sets the current test order.

Here are the available order:

  • :alphabetic Default. Tests are sorted in alphabetic order.
  • :random Tests are sorted in random order.
  • :defined Tests are sorted in defined order.

So I thought this would execute the tests in order of method definition:

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

But when I execute it (tested with test-unit 2.4.9 and 2.5), I get the alphabetic order:

Started
:a
.:b
.:c
.

What's the problem? Is there something missing in my code, is the documentation wrong or is there a bug?

Community
  • 1
  • 1
knut
  • 27,320
  • 6
  • 84
  • 112
  • I know it's obvious, but I'll point it out anyway: depending on the order in which tests are run most probably means that something's wrong. – s.m. Jun 25 '12 at 08:17
  • @s.m. Yes I kno, I try to avoid it. But sometimes I prefer to get a predefined order, so my _more important_ tests are tested first. I could order my tests with method name, but then I rely on the alphabetical order. – knut Jun 25 '12 at 08:51
  • I found this pull request in meantime: https://github.com/seattlerb/minitest/pull/122 There is the expanation, why it is not part of MiniTest. – knut Jun 25 '12 at 08:52

1 Answers1

3

I detected the solution, or better my fault:

gem 'test-unit'
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 difference: I used test_order = :defined in my class. What happened: A local variable test_order was created.

With self.test_order = :defined the method test_order= is called.

knut
  • 27,320
  • 6
  • 84
  • 112
  • My problem ended up being I am using an older version of TestCase that doesn't support test_order: http://www.ruby-doc.org/stdlib-1.8.7/libdoc/test/unit/rdoc/Test/Unit/TestCase.html Thank you though, at least it helped me conclude I was actually setting that var correctly and something else must be wrong (bounty awarded!) – 0xdabbad00 Dec 14 '12 at 09:46