Cut down some seconds of your spec suite by removing database_cleaner
The gem database_cleaner
is a beast and may eat some seconds out of your rspec test suite. By using a simpler approach you can save up some time.
Here's how:
RSpec.configure do |config|
config.around do |example|
# For examples using capybara-webkit for example.
# Remove this if you don't use it or anything similar
if example.metadata[:js]
example.run
ActiveRecord::Base.connection.execute("TRUNCATE #{ActiveRecord::Base.connection.tables.join(',')} RESTART IDENTITY")
else
ActiveRecord::Base.transaction do
example.run
raise ActiveRecord::Rollback
end
end
end
end
The gist here.
The check for :js
environment is necessary if you use capybara-webkit
.
Many thanks to @brandonhilkert for pointing it out here.
With this change, my suite got 40% faster!!
Written by Felipe Elias Philipp
Related protips
8 Responses

Thanks Felipe, my suite got a 32% faster with this little change.

Hey, I will check if that improves my suite!
I did a small change due to foreign keys in my database: http://pastie.org/private/wemfrwk6yxva3syd7kcja
I will post the improvement ASAP.

@felipeelias improved only from 38m36 to 37m02 :(

:( @sobrinho have you tried to run truncation only for javascript examples? In your pastie it was for request type...

@felipeelias my application only works with javascript.
There is no complex javascript (only jQuery and jQuery UI) but it have masked inputs, date pickers and modals in almost every screen.
I think that's the cause of my slow suite and not the database :(

@sobrinho oh shoot! :-(

i'm using single connection method:
https://gist.github.com/joxxoxo/5633252
Then i can use rollback even in integration tests with capybara (with the code above or simply config.use_ transactional_ fixtures = true
Am I missing anything?