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

雨燕双飞 提交于 2019-11-30 07:28:14

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.

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
.
Joy Dutta

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!