bolt githublinkedinPage 1Created with Sketch. spots swoosh target triangle-icon twilio twitterweave

Speed Up Your Tests

YuriyJuly 24, 2017

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 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:


Company

Our solution was to change it to this:


Company

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!

This:


Result 1

Became this:


Result 2

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?


Without the cryptoprovider

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

This:


First cryptoprovider attempt

Became this:


Using a simpler cryptoprovider

Awesome!

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!

THINK

out loud

We think out loud on our blog. We share our views on coding, remote work and post regular insights into the freshest tech industry news.

Want more? Join our mailing list.

    So, how can we help you?

    Live chat