Speeding Up Tests

Episode #104 by Teacher's Avatar David Kimura

Summary

Slow tests can slow down your development process. Using parallel_tests, you can speed up your test suite by multiple threads and running the tests in groups.
rails test 4:54

Resources

Summary

# Gemfile
group :development, :test do
  ...
  gem 'parallel_tests'
  gem 'database_cleaner'
end

# config/database.yml
test:
  <<: *default
  database: db/test<%= ENV['TEST_ENV_NUMBER'] %>.sqlite3

# spec/support/database_cleaner.rb
# Don't forget to add the below comment to your rails_helper.rb
# require 'support/database_cleaner' or 
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
 
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end
 
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end
 
  config.before(:each, type: :feature) do
    DatabaseCleaner.strategy = :truncation
  end
 
  config.before(:each) do
    DatabaseCleaner.start
  end
 
  config.after(:each) do
    DatabaseCleaner.clean
  end
 
end