Run multiple tests in one script in parallel using Ruby Test Unit
Asked Answered
E

3

6

I have 4 tests in one ruby script, which I run using command

ruby test.rb

the out put looks like

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

What I want to achieve is, run all the 4 tests in parallel instead of it being sequential. Something like 4 threads each running one test, effectively reducing the execution time to the slowest of the 4 tests +little time of the parallel execution.

I came across this, but this seems to run multiple ruby test FILES in parallel - say if i had test1.rb, test2.rb test3.rb, then all the three files would run in parallel.

Any help will be appreciated.

Explicit answered 11/5, 2012 at 17:56 Comment(1)
I can confirm that parallel_tests works at the file level, which isn't what you want here, but is very good at what it does.Brack
N
1

I tried a combination of TestSuite and 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)}

It looks good, but I made no benchmark tests.

The output (see below) is a bit chaotic, each thread is posting his messages into the message of other threads, but it seem to work correct. So maybe you must catch the output of each thread to get better test logs.

Loaded suite Test 4Loaded suite Test 1Loaded suite Test 2Loaded suite Test 3
Started
Started
.
Started
.
Started

.
.
Finished in 0.328125 seconds.

Finished in 0.328125 seconds.




1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
Finished in 0.765625 seconds.
Finished in 0.546875 seconds.
100% passed
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications



3.05 tests/s, 3.05 assertions/s
100% passed
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

3.05 tests/s, 3.05 assertions/s

100% passed
100% passed
Namara answered 11/5, 2012 at 21:3 Comment(2)
See also #56587 for threads in ruby.Namara
thanks for your detailed response. This seems like it would work, I am still stuck with this error though. /opt/local/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/ext/module.rb:36:in const_missing': uninitialized constant MyTest (NameError)`Explicit
B
0

It's supposedly possible to run tests in parallel in Ruby 1.9.3, but I haven't got that to work yet.

Bayonne answered 13/5, 2012 at 6:37 Comment(0)
C
0
gem install parallel_tests

parallel_test a_test.rb b_test.rb
Constellation answered 3/7, 2012 at 14:40 Comment(1)
This requires the tests to be in different filesJopa

© 2022 - 2024 — McMap. All rights reserved.