35

除了自动运行所有测试用例之外,有没有办法在 ruby​​ 测试/单元框架下执行单个测试。我知道我可以通过使用 Rake 来实现这一点,但我现在还没有准备好切换到 rake。

ruby unit_test.rb  #this will run all the test case
ruby unit_test.rb test1 #this will only run test1
4

2 回答 2

49

您可以在命令行上传递 -n 选项来运行单个测试:

ruby my_test.rb -n test_my_method

其中 'test_my_method' 是您要运行的测试方法的名称。

于 2011-06-29T03:09:06.030 回答
9

如果您寻找非 shell 解决方案,您可以定义一个 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
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)

在现实生活中,测试类 MyTest 将从原始测试文件中加载。

于 2011-10-11T21:16:15.773 回答