Rspec, Cucumber: best speed database clean strategy

后端 未结 4 1724
庸人自扰
庸人自扰 2021-02-05 14:17

I would like to increase the speed of my tests.

  1. Should I use use_transactional_fixtures or go with the database_cleaner gem?
  2. Whi
4条回答
  •  长发绾君心
    2021-02-05 15:09

    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.

提交回复
热议问题