Rails tests: One line to seriously pump up the speed

Alright, I admit it: I didn’t seriously write tests for Bonanzle until a couple months ago. But I had my reasons, and I think they were good ones.

Reason #1 was that I hated everything about fixtures. I hated creating them, I hated updating them every time we migrated, and I hated remembering which fixture corresponded to which record. Factory Girl was the panacea for this woe.

Reason #2 was that it took an eon to run even a single test. When trying to iterate tests and fixes, this meant that I ended up spending my time 10 parts waiting to one part coding. After much digging, I eventually determined that 90% of our test load time was attributable to caching all our classes in advance. Of course, my first inclination was just not not cache classes in our test environment, which actually worked reasonably well to speed tests the hell up, until I started writing integration tests, and found our models getting undefined and unusable over the course of multiple requests. Then, I found the answer:


This line basically says, even if you set config.cache_classes = true, Rails should not try to pre-load all models (which, in our case is more than 100).

Adding this line allows us to cache classes in test (which fixes the integration test problems), while at the same time getting the benefits of a configuration that doesn’t take 2 minutes to load.

(Of course, also key was configuring our test rakefile such that we could run single tests, rather than being obligated to run the entire suite of tests at once. If anyone needs finds this post and doesn’t yet know how to invoke a single test, post a comment and I’ll get unlazy and post the code for that)

3 Replies to “Rails tests: One line to seriously pump up the speed”

  1. Brought my test suite from 60 seconds to 50 seconds. However, more pressing is the runtime of our tests, and the sheer number that we run (626 for main Rails app, 100 for our proprietary plugins).

  2. Tried this and I ended up with:

    uninitialized constant Client (NameError)

    This is coming from a rails_admin initializer. Is this to be expected? Something else i need to do to get this to work?

    This does seem a bit too good to be true. Why are people wrestling with spork if just this one line would speed up startup time?


Leave a Reply

Your email address will not be published. Required fields are marked *