Using Test::Unit::Testcase for multiple execution

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

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

  1. Make the test itself run ten times

def test_foo
10.times { … }
end

  1. 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<----

  1. 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.

Thanks Brian!
Answer 2. seems to do the job for me, because I can separate the (ten)
calls from the testcases/testsuite. And I will try it out!

Bye
Gerrit

Brian C. wrote in post #1072073:

Gerrit Leder wrote in post #1071964:

  1. 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<----

HTH,

Brian.

Horay, it works! Though I had to make that a comment:
#require ‘test/unit/autorunner’

Thanks again
Gerrit

Gerrit L. wrote in post #1072082:

Thanks Brian!
Answer 2. seems to do the job for me, because I can separate the (ten)
calls from the testcases/testsuite. And I will try it out!

Bye
Gerrit

Brian C. wrote in post #1072073:

Gerrit Leder wrote in post #1071964:

  1. Call the test runner ten 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<----