【发布时间】:2012-06-02 00:22:54
【问题描述】:
我们正在 Ruby 1.9.3 和 Rails 3.2.1 上开发应用程序。
最近,我们的单元测试开始变得迟缓。调用和执行大约需要 15 秒。一旦我看到“Execute test:units”,我又需要 10 秒钟才能看到任何输出。最后,任务完成,测试只需 3 秒即可自行执行。
3 秒的单元测试是可以接受的。 25 秒的加载时间对于 BDD/TDD 来说是不现实的。
当我使用rake test:units --trace 运行时会发生以下情况:
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:test:load_schema (first_time)
** Invoke db:test:purge
** Execute db:test:load_schema
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:prepare
** Execute test:units
我不会怀疑正在加载然后重新加载的数据库架构可能是导致速度缓慢的关键原因。但是,我没有对我们的Rakefile 做任何与单元测试相关的事情。我在哪里可以窥视幕后的真实情况?
这是test/test_helper.rb顶部的内容:
require 'simplecov'
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
我尝试过的:
- 注释掉
simplecov,因为我们只将它用于我们的 CI 服务器,而不是在开发期间(执行时间没有差异) - 注释掉其他东西,但这会破坏测试甚至执行。我不完全确定为什么还有其他项目,因为我认为 Rails 环境会自动加载到“测试”中。
关于我可以在哪里/如何探出头来查看的任何想法?
Rails 是否有可能运行迁移而不仅仅是 schema:load?随着我们的发展,我们有大量的迁移 (~30)。
编辑:
我使用的是 2011 年型号的 Macbook Pro,配备 8G RAM 和 Core i7 - 不要认为这是我的机器。我已经看到in this question 在 Windows 上,require 会导致问题。
我也认为夹具可能是问题,但如果夹具是问题,那么测试本身,而不是测试的加载时间,会很慢,对吧?
编辑 2:
感谢 pchap10k 的回答,我认为这与 rake 无关——这与我们的 Gemfile 有关。也许我应该在那个区域搜索......
【问题讨论】:
标签: ruby-on-rails-3 unit-testing rake testunit