From: Tachikoma Date: 2008-07-16T13:40:22+09:00 Subject: Re: Is it possible to dynamically extend Test::Unit test cases? On Jul 15, 3:19 pm, David Mitchell wrote: > Hello list, > > I've got some "normal" test cases that look like this > > class Testcases < Test::Unit::TestCase > >   def setup >     ... >   end >   def teardown >     ... >   end >   def test_1 >     ... >   end >   def test_2 >     ... >   end > end > > The test cases run as expected, but now I need to add some new test > cases at run time.  What I think I need to do is something like this > > require 'testcases' > t = Testcases.new > t.send(:define_method, "test_defined_at_run_time", "") > > but I'm getting "wrong number of arguments (0 for 1)" when I try to > create the instance of Testcases. > > Is there some way around this? > > Thanks in advance > > David Mitchell I took the following test: require 'test/unit/ui/console/testrunner' require "test/unit" class TestCases < Test::Unit::TestCase def test_case_1 p "case 1" end end eval "class TestCase2 < TestCases;def test_case_2;p 'case 2';end;end;" Test::Unit::UI::Console::TestRunner.run(TestCases) Test::Unit::UI::Console::TestRunner.run(TestCase2) It gives: Loaded suite TestCases Started "case 1" . Finished in 0.000433 seconds. 1 tests, 0 assertions, 0 failures, 0 errors Loaded suite Started "case 1" ."case 2" . Finished in 0.001912 seconds. 2 tests, 0 assertions, 0 failures, 0 errors It works well.and following is the same require 'test/unit/ui/console/testrunner' require "test/unit" class TestCases < Test::Unit::TestCase def test_case_1 p "case 1" end end test_case_2 = Class.new(TestCases) do def test_case_2 p "case 2" end end Test::Unit::UI::Console::TestRunner.run(TestCases) Test::Unit::UI::Console::TestRunner.run(test_case_2) if you take a look into the testcase.rb def initialize(test_method_name) so,I wrote the following code: require 'test/unit/ui/console/testrunner' require "test/unit" class TestCases < Test::Unit::TestCase def test_case_1 p "case 1" end def test_case_2 p "case 2" end end test_case_2 = TestCases.new(:test_case_2) Test::Unit::UI::Console::TestRunner.run(TestCases) Test::Unit::UI::Console::TestRunner.run(test_case_2) the result: Loaded suite TestCases Started "case 1" ."case 2" . Finished in 0.00051 seconds. 2 tests, 0 assertions, 0 failures, 0 errors Loaded suite test_case_2(TestCases) Started "case 2" . Finished in 0.000314 seconds. 1 tests, 0 assertions, 0 failures, 0 errors so the initialize function takes a methods name,and only the method is running if you run the instance but if you run the Class,the test* methods will all run through the TestCase.suite [testcase.rb] I hope this will help