Rspec, Cucumber: best speed database clean strategy
cucumber, database, performance, rspec, ruby-on-rails
Solution
Using transactional fixtures will be faster since the DBMS doesn't commit changes (and therefore no heavy IO occurs resetting the database between tests) but as you know won't always work.
We have had some success using SQLite in-memory databases in the test environment so tests run super fast while leaving transactional fixtures off. This option is also available for MySQL (use :options to set "ENGINE=MEMORY") but I've never done it personally and if you search you'll find a few threads about caveats involved. Might be worth a look. Depending on your testing methodology it may not be acceptable to use a different DB engine though.
I suggest you enable transactional fixtures and use the DatabaseCleaner gem to selectively disable transactional fixtures per example group. I can't say that I've tried this but since you didn't have any answers I figured anything might potentially help you out.
before(:all) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
before(:each) do
DatabaseCleaner.start
end
after(:each) do
DatabaseCleaner.clean
end
If it were me I'd factor this out into a helper and call it as a one-line macro from each example group that needs transactional fixtures turned off.
Seems like there really should be a better way, though.... best of luck.
Problem
I would like to increase the speed of my tests. - Should I use `use_transactional_fixtures` or go with the `database_cleaner` gem? - Which database_cleaner strategy is the best? I noticed that after migration from `:truncation` to `:transaction` my more than 800 examples run about 4 times faster! - Should I turn off `use_transactional_fixtures` when I use database_cleaner `:transaction`? - Is it true that the best strategy for rack_test is `:transaction`? - What is the best practices for changing strategy on the fly from `:transaction` to `:truncation` when using selenium or akephalos? P.S. Mysql, Rails 3, Rspec2, Cucumber P.P.S. I know about spork and parallel_test and using them. But they are offtopic. For example, Spork save about 15-20 sec on whole suite run, but changing from `:transaction` to `:truncation` dramatically increase running time from 3.5 to 13.5 minutes (10 minutes difference).