class ActiveSupport::TestCase
def parallelize(workers: :number_of_processors, with: :processes)
The threaded parallelization uses minitest's parallel executor directly.
parallelize(workers: :number_of_processors, with: :threads)
database and will not work with system tests at this time.
method. Note the threaded parallelization does not create multiple
use threads instead you can pass with: :threads to the +parallelize+
The default parallelization method is to fork processes. If you'd like to
set to the actual core count on the machine you are on.
If +workers+ is set to +:number_of_processors+, the number of workers will be
parallelized.
If the number of workers is set to +1+ or fewer, the tests will not be
you do for local testing.
environments, or other environments where you may need more workers than
and the environment variable will be used instead. This is useful for CI
If ENV["PARALLEL_WORKERS"] is set the workers argument will be ignored
test-database-1
test-database-0
with the worker number.
is forked. For each process a new database will be created suffixed
Takes a +workers+ argument that controls how many times the process
Parallelizes the test suite.
def parallelize(workers: :number_of_processors, with: :processes) workers = Concurrent.physical_processor_count if workers == :number_of_processors workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"] return if workers <= 1 executor = case with when :processes Testing::Parallelization.new(workers) when :threads Minitest::Parallel::Executor.new(workers) else raise ArgumentError, "#{with} is not a supported parallelization executor." end self.lock_threads = false if defined?(self.lock_threads) && with == :threads Minitest.parallel_executor = executor parallelize_me! end
def parallelize_setup(&block)
end
# create databases
parallelize_setup do
class ActiveSupport::TestCase
In your +test_helper.rb+ add the following:
Note: this feature is not available with the threaded parallelization.
but before the tests run.
databases or any behavior that needs to be run after the process is forked
Set up hook for parallel testing. This can be used if you have multiple
def parallelize_setup(&block) ActiveSupport::Testing::Parallelization.after_fork_hook do |worker| yield worker end end
def parallelize_teardown(&block)
end
# drop databases
parallelize_teardown do
class ActiveSupport::TestCase
In your +test_helper.rb+ add the following:
Note: this feature is not available with the threaded parallelization.
the tests finish. This runs before the forked process is closed.
if your app uses multiple write/read databases or other clean up before
Clean up hook for parallel testing. This can be used to drop databases
def parallelize_teardown(&block) ActiveSupport::Testing::Parallelization.run_cleanup_hook do |worker| yield worker end end
def test_order
Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
ActiveSupport::TestCase.test_order # => :random
Returns the order in which test cases are run.
def test_order ActiveSupport.test_order ||= :random end
def test_order=(new_order)
* +:sorted+ (to run tests alphabetically by method name)
* +:parallel+ (to run tests in parallel)
* +:random+ (to run tests in random order)
Valid values are:
ActiveSupport::TestCase.test_order = :random # => :random
Sets the order in which test cases are run.
def test_order=(new_order) ActiveSupport.test_order = new_order end