DEV Community

Cover image for Parallel Testing in Rails 7: Benefits and Pitfalls
Phil Smy
Phil Smy

Posted on

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 database setups. Rails 7, however, introduces a default threshold (50 tests) to decide when to parallelize. Adjust this threshold if required:

config.active_support.test_parallelization_threshold = 100
Enter fullscreen mode Exit fullscreen mode

c. Random Failures Due to Shared Resources

Race conditions introduced by parallel testing can yield random failures. For instance, when two tests access a shared file, their parallel execution can interfere with each other's operations.

To resolve, ensure tests don't share resources and leverage Tempfiles for files or parallelize_setup to namespace resources.

4. Gradual Adoption Strategy

For projects anticipating challenges with full-blown parallel testing adoption, a staggered approach might be beneficial. This way, developers can introduce parallel testing one test class at a time:

  1. Module Approach: Design a module that, when included by a test class, makes it run tests in parallel.

    module Parallelize
      def self.included(base)
        base.class_eval do
          parallelize(workers: :number_of_processors)
          # ... other setups
    class MyTest < ActiveSupport::TestCase
      include Parallelize
  2. Inheritance Approach: Create a test class that runs tests in parallel and inherit from it for tests that are ready for parallelization.

    class ParallelTest < ActiveSupport::TestCase
      parallelize(workers: :number_of_processors)

5. Conclusion

Parallel testing in Rails 7 is undeniably a potent tool to accelerate test suites. With proper understanding and measures, developers can fully harness its power while sidestepping potential pitfalls. Whether adopting it fully or in stages, the key is to ensure that resources are accessed safely and that intermittent test failures are addressed appropriately.

Talk to me!

You can find me on Twitter where I share insights on Ruby on Rails, discuss my journey with Zonmaster, and explore various aspects of life. You can also check out my YouTube channel where I cover various topics related to web development, including Ruby on Rails.

Looking for more? I’ve got an ever-growing series of Ruby on Rails guides over on my Gumroad store! Everything from “Getting Started with Ruby on Rails: A Step-by-Step Guide for Beginners” up to topics like integrating OpenAI’s ChatGPT. Don’t miss out on this opportunity to level up your web development skills with Rails!

Drop me a note on Twitter or LinkedIn if you have any questions or need help with your Rails project. Happy coding! 😊🎉

Top comments (0)