30

如题。

ruby test/functionals/whatevertest.rb 不起作用,这需要我全部替换require 'test_helper'require File.dirname(__FILE__) + '/../test_helper'. 出于某种原因,这些测试模板中的大多数都有这样的问题,所以我宁愿看看是否有一个我可以绕过它的 hack。

4

7 回答 7

48

以下答案基于:如何从 Rails 测试套件运行单个测试?(堆栈溢出)

但很简单,这是答案:

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'

最重要的是,请注意-nswitch 的参数是测试的名称,并在其前面加上“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
于 2011-11-02T19:35:13.830 回答
13

试试这个:

ruby -Ilib:test 测试/功能/whatevertest.rb

于 2009-02-13T21:43:19.697 回答
9

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

于 2008-11-08T07:50:23.767 回答
5

如果您使用的是 Rails 4,那么 rake 支持文件/目录参数。例子:

rake test test/unit/user_test.rb
rake test test/unit
于 2014-02-06T09:20:00.553 回答
4

标题问题的答案是:

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

但是对于问题的主体,tvanfosson给出了很好的答案。

于 2010-08-26T17:25:35.870 回答
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/"
于 2012-09-05T13:45:23.863 回答
0

2和3最常规的方法是:

ruby -I 测试测试/功能/your_test_file.rb

于 2012-04-27T07:35:13.300 回答