How do I execute a single test using Ruby test/unit?
Asked Answered
P

2

37

Instead of running all the test cases automatically, is there any way to execute a single test under ruby test/unit framework. I know I can achieve that by using Rake but I am not ready to switch to rake at this moment.

ruby unit_test.rb  #this will run all the test case
ruby unit_test.rb test1 #this will only run test1
Paraffin answered 29/6, 2011 at 3:4 Comment(0)
C
51

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

ruby my_test.rb -n test_my_method

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

Completion answered 29/6, 2011 at 3:9 Comment(4)
The full option is --name if you prefer long options.Finalize
Also support regex: ruby my_test.rb -n /test_.*/Bifoliolate
Is there a way to run multiple methods? Say, I have 20 methods in the file and I want to run only 5 of it...Bickford
If you are using test "foo bar" do;end in your code, the method name would be: test_foo_barJaynejaynell
C
9

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

Example:

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
end
# >>>>>>>>>>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
Test::Unit::UI::Console::TestRunner.run(my_tests)

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

Cardio answered 11/10, 2011 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.