运行一些测试后,我确信我的设置(windows、rubymine 和最新的 ruby 版本)一定有问题。我现在的时间是:
Finished tests in 14.289817s, 0.0700 tests/s, 0.3499 assertions/s.
1 tests, 5 assertions, 0 failures, 0 errors, 0 skips
Process finished with exit code 0
有 5 个非常简单的测试(只是检查对空字段的验证是否有效)。这 5 个单元测试的总时间为 160 秒,超过 2 分钟。
我可以做些什么来提高这个速度?
这里是测试:
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
test 'item attributes must not be empty' do
item = Item.new
assert item.invalid?
assert item.errors[:name].any?
assert item.errors[:description].any?
assert item.errors[:image_url].any?
assert item.errors[:rating].any?
end
end
最佳答案
您的问题是 Windows。我们在 Windows 上使用 JRuby,它实际上比 Windows 上的 RubyInstaller(mingw) ruby 运行得更快,但在运行测试套件或启动 rails 服务器时我们确实看到非常慢的结果。由于加载 Rails 环境,单次测试运行约 1 分钟。 您有几个选择:
- 切换到 linux/osx
- 使用 spork 为您的测试预加载几个 Rails 环境。请注意,这并不完美,但会大大减少您的时间。有了这个选项,你可能会想要使用 minitest 或 rspec,我很难让 spork 在带有 testunit 的 Windows 上工作。使用 spork,您应该能够将单次测试运行时间减少到大约 10 秒。
- 编写尽可能多的测试以在 Rails 之外运行,换句话说,不需要 Rails 堆栈。这将非常快,您应该能够在几秒钟内运行测试,但正如您所猜测的那样,很难在 Rails 之外测试很多东西( Controller 、 View )。尽管对于您已经分解为不需要来自 rails 的任何内容的模块的功能,它仍然可以完美运行。
祝你好运!
关于ruby - Rake 测试运行很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819770/