DEV Community

Cover image for How to save time when run RSpec tests
Alan Ferreira
Alan Ferreira

Posted on

How to save time when run RSpec tests

two tips that help a lot to save time when running test suites on Rails with RSpec.

1. Reduce Devise.stretches

Note: This tip is useful if you are using Devise as an
authentication tool.

In your spec/test.rb file, add the following line:

Devise.stretches = 1

When using Devise, the cost value is set by a class variable called stretches, with a default value of 11. It specifies the number of times the password is hashed. By setting this value lower, you make the hash algorithm less costly and time-consuming, saving time in the test suite as it's unnecessary to have such a secure password in our testing environment.

2. Increase log level in the test environment

In your spec/test.rb file, add the following line:

Rails.logger.level = 4

Rails logs everything that happens in your test environment by default to "log/test.log". By increasing the logger level, you reduce IO during your tests. The downside is that if a test fails, nothing will be logged. In such cases, simply comment out the above configuration option and rerun your tests.

These two tips already help you save a lot of time when running the test suite with RSpec. Hope this helps!

Top comments (1)

Collapse
 
pimp_my_ruby profile image
Pimp My Ruby

Hi mate,

Devise.stretches = 1 is set by default on the devise config file for test env.
But adding Rails.logger.level = 4 to my spec/rails_helper.rb reduce my suit test time by a solid 1second. Thanks for sharing :)