【问题标题】:Why does Ruby run only some tests and not others?为什么 Ruby 只运行一些测试而不运行其他测试?
【发布时间】:2013-02-23 02:02:26
【问题描述】:

我正在使用这个命令来运行一些测试...

bundle exec ruby -Itest test/functional/*.rb

在我的test/functional 目录中,我有两个文件...

file_sets_controller_test.rb
user_sessions_controller_test.rb

使用上述命令,file_sets_controller_test.rb 中的测试全部运行,但 user_sessions_controller_test.rb 中的测试根本不运行——没有报告错误或其他输出。

不过,我可以直接运行那个文件没问题,用这个...

bundle exec ruby -Itest test/functional/user_sessions_controller_test.rb

效果很好。

我知道另一种选择是使用rake test functionals,但这与直接运行它们相比非常慢。


ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2]

Rails 3.2.12

这是我的 Gemfile 的一部分...

group :development, :test do

  gem 'ansi'
  gem 'turn'

  gem 'minitest'
  gem 'minitest-matchers'

end

这是我的test_helper.rb...

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

require 'turn/autorun'
Turn.config.ansi = true

require 'minitest/autorun'

class ActiveSupport::TestCase

  fixtures :all

end

据我所知,移除 Turn 和 Minitest 宝石并没有改变任何东西。

【问题讨论】:

  • 你知道rake test:functionals,对吧?

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


【解决方案1】:

ruby 命令将要运行的 ruby​​ 文件作为其第一个参数,并使 ruby​​ 程序可以使用其他参数。 shell 将您的 glob 表达式扩展为 2 个参数并将它们传递给 ruby,因此 ruby 正在运行扩展中的第一个文件名。

补充:

我认为你可以用......做你想做的事

bundle exec ruby -Itest -e "Dir.glob('test/functional/*_test.rb').each{|f| require File.expand_path(f)}"

【讨论】:

  • 确定吗?当我运行测试时(在旧版本的 Rails 中),该命令始终将所有文件作为 one 字符串,文件在字符串中用空格分隔。
  • 糟糕,这是我的错误,哦! Steve & 244an 我欠你一杯啤酒。史蒂夫,如果您稍微编辑一下答案,我可以将我的反对票改为赞成票。 (该命令适用于我的设置,因为我有拦截 glob 的代码)。
  • @joelparkerhenderson 我不清楚您建议进行什么编辑。你能澄清一下吗?
猜你喜欢
  • 2011-10-20
  • 2015-07-11
  • 2016-02-19
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多