以下答案基于:How to run single test from rails test suite? (stackoverflow)
但很简单,答案如下:
ruby -I test test/functional/whatevertest.rb
对于特定 functional 测试,运行:
ruby -I test test/functional/whatevertest.rb -n test_should_get_index
只需将下划线放在测试名称中的空格位置(如上),或引用标题如下:
ruby -I test test/functional/whatevertest.rb -n 'test should get index'
请注意,对于unit 测试,只需将上面示例中的functional 替换为unit。如果您使用 bundler 来管理应用程序的 gem 依赖项,则必须使用 bundle exec 执行测试,如下所示:
bundle exec ruby -I test test/unit/specific_model_test.rb
bundle exec ruby -I test test/unit/specific_model_test.rb -n test_divide_by_zero
bundle exec ruby -I test test/unit/specific_model_test.rb -n 'test divide by zero'
最重要的是,请注意-n 开关的参数是测试的名称,并在其前面加上单词“test”,带有空格或下划线取决于您是否引用名称。原因是test 是一种方便的方法。以下两种方法等价:
test "should get high" do
assert true
end
def test_should_get_high
assert true
end
...并且可以作为以下的任一个执行(它们是等价的):
bundle exec ruby -I test test/integration/misc_test.rb -n 'test should get high'
bundle exec ruby -I test test/integration/misc_test.rb -n test_should_get_high