【问题标题】:How to run a single test from a Rails test suite?如何从 Rails 测试套件运行单个测试?
【发布时间】:2010-12-03 04:03:06
【问题描述】:

如何从 Rails 测试套件运行单个测试?

rake test ANYTHING 似乎没有帮助。

【问题讨论】:

标签: ruby-on-rails unit-testing rake


【解决方案1】:

注意: 这不会通过rake 运行测试。所以你在Rakefile 中的任何代码都不会被执行。

要运行单个测试,请在 Rails 项目的主目录中使用以下命令:

ruby -I test test/unit/my_model_test.rb -n test_name

这将运行一个名为“name”的测试,该测试在指定文件的 MyModelTest 类中定义。 test_name 是通过获取测试名称,在其前面加上单词“test”,然后用下划线分隔单词而形成的。例如:

class MyModelTest < ActiveSupport::TestCase

  test "valid with good attributes" do
    # do whatever you do
  end

  test "invalid with bad attributes" do
    # do whatever you do
  end
end

您可以通过以下方式运行这两个测试:

ruby -I test test/unit/my_model_test.rb

只是通过

进行的第二次测试
ruby -I test test/unit/my_model_test.rb -n test_invalid_with_bad_attributes

【讨论】:

  • 这对我不起作用(在功能或单元测试中)。我得到0 tests, 0 assertions, 0 failures, 0 errors。导轨 3.0.7。
  • 我个人是正则表达式的粉丝:-n "/good/"。 Shell 转义总是很有趣,所以我倾向于坚持使用简单的正则表达式,但这比一直写出完整的测试名称要容易得多。
  • 重要的是要注意,要使其正常工作,您不应位于响应中指定的 MAIN 目录中,而应位于包含 test/ 文件夹的子目录中。例如,如果我想运行测试activesupport/test/core_ext/array_ext_test.rb,我应该先在activesupport/
  • @Groxx - 终于找到了解决方案!谢谢! -n "/good/" 工作。
  • 在 2018 年及以上运行时要小心。它绕过了一些内置在 rails 中的测试任务(例如 db:test:prepare),这些任务用于许多事情,例如将本地数据交换为夹具数据,然后再次恢复它。运行它会炸毁我的本地数据库。考虑一个使用内置 rails 测试任务的解决方案,例如 stackoverflow.com/a/47006811/1154642(对我有用)。
【解决方案2】:

运行测试文件

rake test TEST=tests/functional/accounts_test.rb

在测试文件中运行单个测试

rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/"

(来自@Puhlze 的评论。)

【讨论】:

【解决方案3】:

对于轨道 5:

rails test test/models/my_model.rb

【讨论】:

  • 但这会运行文件中的所有测试用例,问题要求运行单个测试...
  • 您可以通过将行号附加到文件名来运行单个测试:bin/rails test test/models/user_test.rb:27
【解决方案4】:

感谢@James,答案似乎是:

rails test test/models/my_model.rb:22

假设 22 是给定测试的行号。根据rails帮助:

 $ rails test --help

您可以通过将行号附加到文件名来运行单个测试:

    bin/rails test test/models/user_test.rb:27

另外,请注意,您的测试应该继承自 ActionDispatch::IntegrationTest 才能正常工作(这是我的错误):

class NexApiTest < ActionDispatch::IntegrationTest
.
.
.

【讨论】:

  • 这似乎是基于 Rails 指南的最佳答案,至少对于测试特定行而言。
【解决方案5】:

导轨 5

我用这种方式运行单个测试文件(一个文件中的所有测试)

rails test -n /TopicsControllerTest/ -v

另一种选择是使用行号(打印在失败的测试下方):

rails test test/model/my_model.rb:15

【讨论】:

  • 这是对最新 Rails 版本问题的最佳答案,+1
  • 请注意,一对正斜杠内的单词可以匹配任何方法名称。请注意,任何空格都必须替换为_。例如,-n "/many_num/" 将匹配 test "many numbers" dotest "many numbered items" do 等。
【解决方案6】:

在我的情况下,rake 仅适用于TESTOPTS="-n='/your_test_name/'"

bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/your_test_name/'"

【讨论】:

  • 我也注意到了这一点。过去我可以发誓它在没有 =-n= 中的那个)的情况下工作,但现在它一直认为它是一个文件名。
  • 非常感谢您的回答,现在我终于可以运行特定的测试了...
【解决方案7】:

在实际的 Rails 套件中运行单个测试:

bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb

【讨论】:

    【解决方案8】:

    这是我的一个愚蠢的午夜问题。 Rails 友好地打印出它在rake test 上执行的命令。剩下的就是剪切和粘贴练习了。

    ~/projects/rails/actionpack (my2.3.4)$ ruby -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/controller/base_test.rb"
    

    【讨论】:

      【解决方案9】:

      最好的方法是直接查看指南:http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests

      cd actionmailer
      bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout
      

      【讨论】:

        【解决方案10】:

        如果您想运行单个测试,您可以将它们作为常规 Ruby 脚本运行

        ruby actionmailer/test/mail_layout_test.rb
        

        您还可以通过 cd-ing 进入目录并在其中运行 rake test 来运行整个套件(例如 ActiveRecord 或 ActionMailer)。

        【讨论】:

        • 不在 Rails 中——至少在生成的默认测试文件中没有。他们在第一行有“需要'test_helper'”,但加载路径不会及时设置。如果您将每一行都更改为显式要求(“require File.join(File.dirname(FILE), '..', 'test_helper')”),那么您的解决方案将有效。跨度>
        • 咳咳,那是require File.join(File.dirname(__FILE__), '..', 'test_helper')
        • @Gaius 仔细检查。第一个不起作用,但cd-ing in 起作用。明确一点(我不确定我是否误读了这篇文章,但您确实说过“生成的测试文件”),这是针对 Rails 库本身的,不是 Rails 项目。
        【解决方案11】:

        要重新运行刚刚失败的测试,请将失败的测试名称复制粘贴到

        rails test -n [test-name]
        

        示例

        当您的测试套件报告此情况时:

        > rails test
           ...
           Error:
           PlayersControllerTest#test_should_show_player:
           ActionView::Template::Error: no implicit conversion from nil to integer
        

        你用这个重新运行失败的测试:

        rails test -n PlayersControllerTest#test_should_show_player
        

        【讨论】:

          【解决方案12】:

          首先,访问你要测试的库的文件夹(这很重要),然后运行:

          ~/Projects/rails/actionview (master)$ ruby -I test test/template/number_helper_test.rb 
          

          【讨论】:

            【解决方案13】:

            Rails 文件夹

              bundle install
              bundle exec ruby -I"activerecord/test" activerecord/test/cases/relation/where_test.rb 
            

            注意你需要加载适当的文件夹:“activerecord/test”(你有测试的地方)

            【讨论】:

              猜你喜欢
              • 2016-11-24
              • 1970-01-01
              • 2012-02-17
              • 2016-11-07
              • 2014-03-25
              • 2017-04-20
              • 2011-10-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多