Speed Up Your Tests

After some changes to the database structure on our project, we realised that our specs were going on for rather a long period of time and builds began breaking our workflow. So we set ourselves a challenge: speed up our tests as soon as possible…

First, we attempted to discover why this was happening and we found that our fabricators were working very slowly.

Example of Company fabricator:

Company - as shown above - is an essential model in our project and we use it in every spec example.

Company has a lot of associated ‘documents’ and it means that we fabricate these each time we call create(:company) in specs. Hmm… Wouldn’t it be cool to fabricate company at the beginning of the specs and use it in each example?

In each spec example we were using something similar to this:

Our solution was to change it to this:

You may ask what is the AnyFixture class? Big thanks to Vladimir Dementyev for this solution. You can find the full class implementation here. Basically, it’s just a class with methods that tracks which tables were affected and clean db after test run.

The results were awesome!


Became this:

In fact for one file our speed grew by 18 times. Perfect result!

Also, another issue we experienced was with signing users in spec. By default we use base cryptoprovider to encrypt our users’ passwords, and these provide a strong level of encryption. But do we need it in specs too?

So in specs we started to use a simpler cryptoprovider and the tests became 3 times faster.


Became this:


So in summary, make sure to always follow these two tips when trying to speed up your tests:

  • Look at you fabricators and try to understand why they can be slow
  • Use less complex cryptoproviders in your specs

Thanks for reading!