我们正在 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 - 不要认为这是我的机器。我在这个问题中看到,在 Windows 上,require
可能会导致问题。
我也认为夹具可能是问题,但如果夹具是问题,测试本身,而不是测试的加载时间,会很慢,对吧?
编辑2:
感谢 pchap10k 的回答,我认为这与 rake 无关——它与我们的Gemfile
. 也许我应该在那个区域搜索......