Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Rake loads environment multiple times for tests #3089

commondream opened this Issue Sep 21, 2011 · 7 comments


None yet
6 participants

When running test tasks, Rake loads the environment and then each test execution starts a new process and loads the environment. My environment takes around 15 seconds to load, so the result is an extra 45 seconds of runtime when running rake test:units (one load initially and then 3 extra loads for each test execution). It would be awesome if the environment were only loaded if it were needed, so that that time could be shaved off.

are you sure it's a "Rails" issue? You can try to use Autotest (provide quicker test runs than standard convention since it intelligently monitors the changes and only runs the tests that are impacted by these changes) or Spork to speed up your test process

I put debugging code around the environment load, and the environment is definitely being loaded several times per test run. I'm perfectly fine if it's just the decision to keep loading and reloading the environment, but it seems like it'd be ideal to avoid loading several times if possible.


dchelimsky commented Jan 15, 2012

This happens because the environment is "development" by default. When you type "rake xxx", if the task loads up the Rails environment, Rails sees that "development" and starts setting things up based on config/environments/development.rb. The tests, however, need to run in the "test" environment, so the test tasks shell out to a new process with the "test" environment in order to avoid conflicts (and trying to resolve them) between the development and test environments.

One potential solution would be to initialize the 'test' env for the 'test' task:

task :test => :init_test_env do
  # ...

task :init_test_env do
  ENV['RAILS_ENV'] ||= 'test'

Now the test task can just run if the env is 'test', or shell out if it is anything else. This is overly simplistic, as we'd probably need some means of whitelisting environments named by end-users or by upstream libs, but it gets the idea across.


I think that's definitely along the right direction. The rake tasks for testing also spawn a new process that loads up the environment, so I think those tasks would have to be rewritten to avoid the extra environment load as well.

@dchelimsky agree. Rspec does this too. It's "problem" with rspec as well, especially if you need to pay the rails initialization cost twice.


dchelimsky commented Apr 29, 2012

@twinturbo with rspec you can type the rspec command and it only loads up the env once. This issue is specific to running tasks with Rake when the default environment is 'development'.


goshakkk commented Jan 6, 2013

I think it can be closed, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment