We frequently think about how good it would be if we could run tests in parallel on local so there would be less wait time for tests to be completed. Wait times increase considerably when the count of tests are on the higher side, which is a common case for a lot of applications.
Before Rails 6, if we wanted to parallelize tests, we would use Parallel Tests.
Rails 6 adds the parallelization of tests by default. Rails 6 added parallelize
as a class method on ActiveSupport::TestCase which takes
a hash as a parameter with the keys
worker key is responsible for setting the number of parallel
workers. The default value of the
worker key is
:number_of_processors, which finds the number of processors on the machine and sets it as the number of
with takes two values -
:processes, which is the default one, and
:threads as a value.
Rails 6 also added two hooks -
parallelize_setup, which is called before the processes are forked, and
is called after the processes are killed. Rails 6 also handles creation of multiple databases and namespacing of those databases for parallel tests
out of the box.
If we want to disable parallel testing, we can set the value of
workers as 1 or less.
Rails 6 also provides an environment variable
PARALLEL_WORKERS to set the number of parallel workers on runtime.
$ PARALLEL_WORKERS=10 bin/rails test