运行单个 Rails 单元/功能测试

31

题目如下。

ruby test/functionals/whatevertest.rb不能工作,需要我将所有的require 'test_helper'替换为require File.dirname(__FILE__) + '/../test_helper'。由于大多数测试模板都存在这样的问题,因此我想看看是否有什么方法可以绕过它。


可能是如何从Rails测试套件中运行单个测试?的重复问题。 - krubo
7个回答

49
以下答案基于:How to run single test from rails test suite? (stackoverflow)
但简要来说,这就是答案:
ruby -I test test/functional/whatevertest.rb

要运行一个具体的功能性测试,执行:

ruby -I test test/functional/whatevertest.rb -n test_should_get_index

只需在测试名称中使用下划线代替空格(如上所示),或按以下方式引用标题:

ruby -I test test/functional/whatevertest.rb -n 'test should get index'

请注意,在上面的示例中,对于功能测试,只需将其替换为单元测试。如果您正在使用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

谢谢,这很有帮助。标准目录是:test/functional/ 而不是 test/functionals/,所以我修改了你的答案。谢谢。 - vdaubry
糟糕..感谢您的编辑!我一定是不小心加了一个_s_,因为有时我会通过bundle exec rake test:functionals只执行功能测试,这是复数形式。注意:运行所有单元测试也是一样的..只需将functionals替换为units即可。 - user664833
我刚刚安装了rails 3.0.7,并创建了一个新的“depot”应用程序(rails new depot),cd depotbundle install(安装sqlite3),rails g scaffold User name:stringbundle exec rake db:migrateRAILS_ENV=test bundle exec rake db:migrate,然后我使用ruby 1.9.2p290运行了以下测试,并且它们都成功了:ruby -I test test/unit/user_test.rbruby -I test test/unit/user_test.rb -n test_the_truth以及ruby -I test test/unit/user_test.rb -n 'test_the_truth'。测试的实际名称是“the truth”。此外,我在应用程序的根目录中。 - user664833
@user664833 - 好的,我完成了这个过程,所有三个命令都有效。我猜测我的gems或者其他项目中的monkey patching破坏了这个功能。它也适用于一个功能测试。需要知道的重要事情是,测试名称必须在前面加上“test”:test_the_truth - B Seven
@BSeven,我已经更新了答案以反映这一点。很抱歉它之前有误导性。 - user664833
显示剩余3条评论

13

请尝试以下命令:

ruby -Ilib:test test/functionals/whatevertest.rb


4
我实际上是这样运行它的: ruby -I test test/functional/whatevertest.rb 但结果都一样。 - Dominik Grabiec

9

在Linux上?为什么不尝试使用(cd test && ruby functionals/whatevertest.rb)。注意,括号很重要,否则您的当前目录将更改为子目录。它所做的是fork另一个shell,在其中切换到子目录并运行测试。


这个不错。我稍微改了一下,变成了(cd test && ruby functional/whatsoevertest.rb),这样就可以正确地处理相对目录结构了。非常合理 :) - William Yeung

5

如果您正在使用Rails 4,则rake支持文件/目录参数。例如:

rake test test/unit/user_test.rb
rake test test/unit

4

标题问题的答案是:

ruby unit/post_test.rb -n selected_test # use to run only one selected test

但是对于问题的内容,tvanfosson 给出了一个很好的答案。


我遇到了以下错误:<internal:lib/rubygems/custom_require>:29:in 'require': no such file to load -- test_helper (LoadError) 它能用于功能测试吗? - B Seven

4

在无数个小时的探索之后,我终于找到了解决方法(在Rails 3.0.7中):

ruby -I test test/functional/users_controller_test.rb -n "/the_test_name/"

请注意,这仅适用于命令中的下划线 (_)。它无法使用空格!

将测试定义为带有空格的内容:

test "the test name" do

这个解决方案使用模式匹配,所以你可以使用测试名称的一部分。如果测试是“应该执行foo”,则以下任何一个也可以使用。

ruby -I test test/functional/alerts_controller_test.rb -n "/foo/"
ruby -I test test/functional/alerts_controller_test.rb -n "/do_foo/"

以下代码(带有空格)将无法正常工作:
ruby -I test test/functional/alerts_controller_test.rb -n "/do foo/"

1
我发现如果在模式中转义“空格”,它就可以工作。因此,类似以下的内容是可行的: ruby -I test test/my_test.rb -n "/do\ foo/" - Oto Brglez
由于某种原因,对我来说留下空格是有效的,我使用的是 ruby -v ruby 1.9.3p484>> Rails::version => "2.3.18" - mswieboda

0

2和3的最常规方法是:

ruby -I test test/functional/your_test_file.rb


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接