The performance measurements in this card needs to be retaken for PostgreSQL.
TLDR: In tests you need to clean out the database before each example. Use :transaction
where possible. Use :deletion
for Selenium features or when you have a lot of MyISAM tables.
You want to clean out your test database after each test, so the next test can start from a blank database. To do so you have three options:
DatabaseCleaner.strategy = :transaction
or config.use_transactional_fixtures = true
)DatabaseCleaner.strategy = :truncation
)DatabaseCleaner.strategy = :deletion
)Additionally there are some constraints for when you can use transactions:
I measured the runtime of different strategies using an average-sized Rails project (with MySQL):
Cucumber | RSpec | |
---|---|---|
Transaction | 87.14, 86.65 | 10.20, 10.11 |
Truncation | 90.43, 90.69 | 26.82, 26.46 |
Deletion | 86.20, 85.61 | 12.08, 12.12 |
What we can take away from this:
:truncation
and replace it with :deletion
. An exception to this rule is when you are using a lot of
foreign key constraints
Show archive.org snapshot
(you probably don't). In that case truncation might outperform deletion (but I didn't measure that).config.use_transactional_fixtures = false
when moving away from transactions. Using both transactions and another cleaning strategy will drag down your test performance by quite a bit.