views:

75

answers:

2

Is it normal for my test suite to take 5 seconds just to launch? Even when running an empty suite it still takes this long. Is it because it's firing up a new instance of rails on each run? If so, is there anyway to keep it persistent?

Example:

rlepidi@rlepidi:~/projects/rails/my_project$ time rake test
/usr/bin/ruby1.9.1 -I"lib:test" "/var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/release_test.rb" 
Loaded suite /var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader
Started

Finished in 0.000181867 seconds.

0 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

real    0m4.173s
user    0m3.820s
sys 0m0.288s

As you can see, this empty test is really fast, but there is still 4 seconds of overhead for some reason. I'm using Test::Unit with Shoulda.

A: 

How long does your test run? Usually Rails tests take long due to database access. To speed them up as initial improvement you could:

  1. stop using instantiated fixtures by setting:

    self.use_instantiated_fixtures = false

  2. start using transactional fixtures by setting:

    self.use_transactional_fixtures = true

To improve tests performance even more please consider using preloaded fixtures.

koppernickus
The amount of time the tests take to actually *run* is negligible. I only have one suite right now, but the problem is even when I have NO tests, it still takes like 5 seconds to run. When I run my one suite it's maybe 5.1 seconds. I'm just trying to figure out if it's supposed to take this long to bootstrap. Does it for you?
ryeguy
I edited my post to show how much time stuff takes.
ryeguy
+3  A: 

Test speed bottleneck is the time it takes Rails to load its environment. Run script/console and see how long it takes to load you into your environment...it should be the same.

One you have a large enough codebase with many plugins & gems, load time can be as high as 1 minute!! If you are like me & stop running tests b/c you are sick of waiting 30 seconds just to test a small change, you need to run a test server like spec_server, or even better: spork!

You will find that there may be strange issues dealing with class cacheing with spec_server which is why I prefer spork.

simianarmy
Thanks, I guess it really was just the Rails environment loading. I've switched to Rspec with Spork and it's a bit better.
ryeguy