ivvntg

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


endorse

8 Responses
Add your response

3026
0e6c0ba9935b52866fd5c54dd886cf2e

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

over 1 year ago ·
3030
C2dc9c02ce7a041285725a4fc9e5f6d2

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.

over 1 year ago ·
3031
F6faa607e4f541da71fbf8aea6f99a7a

@sobrinho thanks! I'm keeping it as a gist here

over 1 year ago ·
3034
C2dc9c02ce7a041285725a4fc9e5f6d2

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

over 1 year ago ·
3036
F6faa607e4f541da71fbf8aea6f99a7a

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

over 1 year ago ·
3037
C2dc9c02ce7a041285725a4fc9e5f6d2

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

over 1 year ago ·
3038
F6faa607e4f541da71fbf8aea6f99a7a

@sobrinho oh shoot! :-(

over 1 year ago ·
6543
B7c760acc14afc2919f67bd0ff46e1ca

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?

over 1 year ago ·