Member-only story
Parallel Testing in Rails 7: Benefits and Pitfalls
Parallel testing has emerged as a popular strategy for speeding up test suites, and with Rails 7, it’s more efficient and effective than ever. However, as with any technology, understanding its benefits and challenges is essential for optimal utilization.

1. Introduction to Parallel Testing in Rails 7
Parallel testing lets you run your test suite in a concurrent manner. By default, Rails 7 forks processes using Ruby’s DRb system. The number of processes forked corresponds to the machine’s core count but can be adjusted.
To activate parallel testing, insert this into test_helper.rb
:
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors)
end
For those using JRuby or desiring threaded parallelization, Rails 7 offers a threaded parallelizer, backed by Minitest’s Parallel::Executor. To employ threads:
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors, with: :threads)
end
2. How Parallel Testing Works in Rails 7
Parallelization divides the test suite based on specified worker count. By default, Active Record handles database creation and schema loading for each process, prefixing databases with corresponding worker numbers.
To modify the worker count during a test run:
PARALLEL_WORKERS=15 bin/rails test
3. Addressing Common Pitfalls
While parallel testing brings promise, be aware of the potential snags:
a. Compatibility with RSpec
In Rails 6, RSpec didn’t support built-in parallel testing. While this has been a point of discussion, if you’re still using RSpec and require parallel tests, you might rely on third-party gems like grosser/parallel_tests
.
b. Overhead with Smaller Test Suites
Sometimes, a minor test suite might run slower in parallel due to overheads like multiple…