How we made our rspec test suite to run 2x faster


Bookmark and Share

 

Why on earth is my test suite taking so long to run?

If you are a developer, you might have asked this question at least once to yourself. So did we, our rails project’s test suite was taking a good 1 hour 30 minutes to run and we wanted to improve that time so badly that we eventually did exactly that, reducing nearly 1 and half hours to just minutes, and this is how we did it.

1. Database independent tests

DB operations are often time-consuming and most of the time we can do away with saving objects to the database to run our tests.

Use test doubles, stubs and mocks instead of creating the real instance of a class and invoking methods on the created instance.

Our test case

This test can be made faster by replacing

with

2) Use gem group

Rails preload your gems before running tests. Using gem groups allow rails to load only the environment specific dependencies.

#Gemfile

3) Use before(:each) and before(:all) hooks carefully

Since before(:each) runs for every test, be careful what we include inside before(:each) hook. If the code inside before(:each) is slow every single test in your file is going to be slow.

A workaround would be …

Read More