# ANN: faster fixtures (or how to spend less time loading fixt

**URL:** <https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959>\
**Category:** Rails\
**Created:** [September 19, 2007, 9:26am UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959 "2007-09-19T09:26:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 19, 2007, 9:26am UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/1 "2007-09-19T09:26:32Z")

</div>

Short version:  
Before:  
Finished in 192.609637 seconds.  
183 tests, 5100 assertions, 0 failures, 0 errors

After:  
Finished in 104.491294 seconds.  
183 tests, 5100 assertions, 0 failures, 0 errors

Ok, so this app does have a lot of big fixtures but we’ve seen decent  
gains on all our apps (typically 30-40%)

The long version:  
fixtures are loaded once per testcase. If every single of your tests  
names 0 or 1 fixtures than that’s fine. Back in the real world, customer  
has\_many :orders, :complaints, orders has\_many :products etc… the  
tests for all of orders, complaints, products, customer probably name  
all those fixtures.

If a model uses another that isn’t named in the fixtures you can get  
weird failures (because the table won’t have been cleared out and had  
fresh data stuck in it prior to running the test, so for reasons of  
maintainability we’ve ended up with all our fixtures in test\_helper.  
This of course means that if you have 50 unit tests, each one is being  
loaded 50 times, i.e. 49 times too many. With some small caveats, this  
plugin addresses this situation

Caveats:

- this is an all or nothing thing: you can’t have it on for some  
testcases, off for others
- all of your fixtures must be named in test\_helper
- time stops (is stubbed out)i.e :  
now = Time.now  
get :index  
assert Time.now \> now

will fail. the point at which time stops is just before fixtures are  
loaded

Setting up:

- make sure you have mocha (gem install mocha)
- grab the plugin from  
[NameBright - Coming Soon](http://svn1.hosted-projects.com/fcheung/faster_fixtures/trunk)
- make sure all your fixtures must be named in test\_helper
- add require ‘faster\_fixtures’ to your test\_helper

Run tests and enjoy!

For another solution to this (but which means you can’t use the fixtures  
accessors (eg foos(:first\_foo) see this)

Thoughts, comments, suggestions etc… appreciated as usual. It works  
great for all our apps, but I’d love to hear about it if it breaks yours  
(and hopefully fix it 🙂 )

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 20, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/2 "2007-09-20T21:50:06Z")

</div>

I’ve just updated this to remove the requirement that all your fixtures  
be in test\_helper.rb. You can now declare them whenever, whereever you  
want and my plugin just insures that any given fixture is not loaded  
more that once.  
I’d love to know if this works for other people (or indeed if it  
doesn’t!)

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 22, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/3 "2007-09-22T18:40:47Z")

</div>

Frederick C. wrote:

> I’ve just updated this to remove the requirement that all your fixtures  
> be in test\_helper.rb. You can now declare them whenever, whereever you  
> want and my plugin just insures that any given fixture is not loaded  
> more that once.  
> I’d love to know if this works for other people (or indeed if it  
> doesn’t!)
> 
> Fred

Slick stuff – a test suite that ran in 50 seconds now runs in 28  
seconds.

Once issue I’ve run across – I’ve got some unit tests in a  
sub-directory under /test/unit/, and they aren’t able to access any  
fixture data when I run the suite with faster\_fixtures required. When I  
run the tests individually, they can access fixture data just fine.

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 22, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/4 "2007-09-22T21:57:09Z")

</div>

I’ve tried to reproduce this but couldn’t. Could you give some more  
details about your setup and the errors you’re getting ?

Thanks,

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 22, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/5 "2007-09-22T21:24:38Z")

</div>

Geoff Buesing wrote:

> Slick stuff – a test suite that ran in 50 seconds now runs in 28  
> seconds.
> 
> Once issue I’ve run across – I’ve got some unit tests in a  
> sub-directory under /test/unit/, and they aren’t able to access any  
> fixture data when I run the suite with faster\_fixtures required. When I  
> run the tests individually, they can access fixture data just fine.

Interesting - I’ll have a look.

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 23, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/6 "2007-09-23T12:55:13Z")

</div>

For a clearer explanation of what’s going on, see

> **[钛刻专家——官网](https://www.texperts.com/2007/09/22/loading-fixtures-with-parsimony/)**
>
> 钛刻专家致力于打造一个全球专家在线服务平台，为机构和个人对接高水平行业专家，为机构提供更系统性的线下定制专家服务。

Fred

---

<div class="post-metadata">

**Author:** ![fcheung](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/90db22/32.png) [@fcheung](https://www.ruby-forum.com/u/fcheung)\
**Post date:** [September 24, 2007, 6:01am UTC](https://www.ruby-forum.com/t/ann-faster-fixtures-or-how-to-spend-less-time-loading-fixt/114959/7 "2007-09-24T06:01:41Z")

</div>

Frederick C. wrote:

> I’ve tried to reproduce this but couldn’t. Could you give some more  
> details about your setup and the errors you’re getting ?
> 
> Thanks,
> 
> Fred

Fred,

Thanks for following up on this.

I created a fresh Rails app to try to reproduce this bug, but  
(consistent with your findings) I wasn’t able to.

I did figure out a workaround – I’m using test/spec for my tests; when  
I change the problem test (the one in a subdirectory of  
RAILS\_ROOT/test/unit) to standard test/unit style, it passes fine. Who  
knows why.

My setup: Ruby 1.8.6, a recent version of edge Rails, with  
test\_spec\_on\_rails and fixture\_references plugins (plus some other  
common plugins that shouldn’t be interfering with testing.)

The problem is only with one particular record in one particular YAML  
fixture file (which looks to be in fine YAML form) – I get  
“ActiveRecord::RecordNotFound: Couldn’t find Location with ID=1” when I  
run the suite, but when I run the individual test, it runs just fine.  
When I check the db after I run the suite, I find three records in the  
corresponding table; after I run the individual test, I see four.

And if I don’t require ‘faster\_fixtures’ in my test.rb, I have no  
problem.

So, who knows…?

Geoff
