使用Ruby测试单元在一个脚本中并行运行多个测试

使用Ruby测试单元在一个脚本中并行运行多个测试,ruby,unit-testing,parallel-processing,testunit,Ruby,Unit Testing,Parallel Processing,Testunit,我在一个ruby脚本中有4个测试,我使用命令运行该脚本 ruby test.rb 输出看起来像 Loaded suite test Started .... Finished in 50.326546 seconds. 4 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 100% passed 我想要实现的是,并行运行所有4个测试,而不是按顺序运行。 类似于4个线程,每

我在一个ruby脚本中有4个测试,我使用命令运行该脚本

ruby test.rb
输出看起来像

Loaded suite test
Started
....

Finished in 50.326546 seconds.

4 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
我想要实现的是,并行运行所有4个测试,而不是按顺序运行。 类似于4个线程,每个线程运行一个测试,有效地将执行时间减少到4个测试中最慢的一个,并且并行执行的时间很少

我遇到过这样的问题,但这似乎是并行运行多个ruby测试文件——比如说,如果我有test1.rb、test2.rb和test3.rb,那么这三个文件都将并行运行


任何帮助都将不胜感激。

我尝试了
TestSuite
Thread
的组合:

gem 'test-unit'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
# we're running the tests, so we don't want Test::Unit to automatically run everything for us.  See http://www.natontesting.com/2009/07/21/stop-rubys-testunit-suite-files-running-all-your-tests/
Test::Unit.run = true


class MyTest < Test::Unit::TestCase
  def test_1()
    assert_equal( 2, 1+1)
  end
  def test_2()  
    assert_equal( 2, 4/2)
  end
  def test_3()      
    assert_equal( 1, 3/2)
  end
  def test_4()  
    assert_equal( 1.5, 3/2.0)
  end
end

#create TestSuites.
test_1 = Test::Unit::TestSuite.new("Test 1")
test_1 << MyTest.new('test_1')
#Same a bit shorter
test_2 = Test::Unit::TestSuite.new("Test 2") << MyTest.new('test_2')
test_3 = Test::Unit::TestSuite.new("Test 3") << MyTest.new('test_3')
test_4 = Test::Unit::TestSuite.new("Test 4") << MyTest.new('test_4')


#run the suites
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_1)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_2)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_3)}
Thread.new{Test::Unit::UI::Console::TestRunner.run(test_4)}

在Ruby 1.9.3中可以并行运行测试,但我还没有做到这一点。

另请参阅Ruby中的线程。感谢您的详细回复。这似乎是可行的,但我仍然坚持这个错误
/opt/local/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in
const_missing':未初始化常量MyTest(namererror)`我可以确认并行_测试在文件级工作,这不是您想要的,但在这方面做得非常好。这要求测试位于不同的文件中
gem install parallel_tests

parallel_test a_test.rb b_test.rb
gem install parallel_tests

parallel_test a_test.rb b_test.rb