[ACCEPTED]-How do I execute a single test using Ruby test/unit?-testunit

Accepted answer
Score: 49

you can pass the -n option on the command 3 line to run a single test:

ruby my_test.rb -n test_my_method

where 'test_my_method' is 2 the name of the test method you would like 1 to run.

Score: 9

If you look for a non-shell solution, you 3 could define a TestSuite.


gem 'test-unit'
require 'test/unit'
require 'test/unit/ui/console/testrunner'

#~ require './demo'  #Load the TestCases
# >>>>>>>>>>This is your test file demo.rb
class MyTest < Test::Unit::TestCase  
  def test_1()
    assert_equal( 2, 1+1)
    assert_equal( 2, 4/2)

    assert_equal( 1, 3/2)
    assert_equal( 1.5, 3/2.0)
# >>>>>>>>>>End of your test file  

#create a new empty TestSuite, giving it a name
my_tests = Test::Unit::TestSuite.new("My Special Tests")
my_tests << MyTest.new('test_1')#calls MyTest#test_1

#run the suite

In real 2 life, the test class MyTest will be loaded 1 from the original test file.

More Related questions