Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rake loads environment multiple times for tests #3089

Closed
commondream opened this Issue · 7 comments

6 participants

@commondream

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.

@mikhailov

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

@commondream

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

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
  # ...
end

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

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.

Thoughts?

@commondream

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.

@ahawkins

@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

@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

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
Something went wrong with that request. Please try again.