Gerrit Leder wrote in post #1071964:
Hello all,
I have written a Testcase based upon Test::Unit::Testcase and want to
execute it repeatedly, say 10.times
But the testcase is not executed multiple times, when loaded subsequent
times with “load”.
I tried “Thread”, “Proc”, “require”, “load” and similar to execute the
testcase, but it is always running only once.
How can I execute a testcase with the same name in a loop or something?
Thanks
Gerrit
- Make the test itself run ten times
def test_foo
10.times { … }
end
- Call the test runner ten times
To do this, don’t rely on the default behaviour. That is: when you
require ‘test/unit’, it finds all class objects derived from
Test::Unit::TestCase and runs them once.
Notice the following code at the end of test/unit.rb (this may be
installed somewhere like /usr/lib/ruby/1.8/test/unit.rb)
at_exit do
unless $! || Test::Unit.run?
Kernel.exit Test::Unit::AutoRunner.run
end
end
So now you can see how it works, you can run the test suite 10 times:
----8<----
require ‘test/unit/testcase’
require ‘test/unit/autorunner’
class TestFoo < Test::Unit::TestCase
def test_foo
assert true
end
end
10.times { Test::Unit::AutoRunner.run }
----8<----
- However, running the suite 10 times is not exactly the same as
running one suite with each test invoked 10 times.
To do that, you probably want to make a variation on
Test::Unit::TestSuite or Test::Unit::TestCase which runs each test 10
times. Looking through the code, it’s clear that there’s a ‘run’ method
on both TestSuite and TestCase, and since you subclass TestCase already
there’s no need for monkey-patching the underlying class.
----8<----
require ‘test/unit’
class TestFoo < Test::Unit::TestCase
alias :orig_run :run
def run(*args,&blk)
10.times { orig_run(*args,&blk) }
end
def test_foo
assert true
end
end
----8<----
$ ruby ert.rb
Loaded suite ert
Started
…
Finished in 0.000861 seconds.
10 tests, 10 assertions, 0 failures, 0 errors
HTH,
Brian.