Setup Rspec parallel tests with Circle CI

While working on Rspec tests on a Ruby on Rails project, I realised the test suite is taking a lot of time. This was causing the pipelines run slow and resulting in slower development cycle. We had to make it a bit faster.

While Rails has inbuilt support for parallelization now, it is available for Minitest testing framework. To work with Rspec, we need to use gems like parallel_spec, turbo_test (a solution came out of project discource). In this article, we will explore working with parallel_tests gem and integrate it with Circle CI as well.

Setup gem

Add gem to Gemfile

gem 'parallel_tests', group: [:test]

Now, install the gem to use it with your testing framework.

bundle install

Run specs in parallel

Now, you can run specs in parallel using following command.

bundle exec rake parallel:spec

This will divide the specs (examples) in your application in number of processes. The number of processes are figured out based on the number of cores available on the machine running tests.

We can specify number of processes to be used explicitly as given below.

bundle exec rake parallel:spec"[2]"

This will use 2 processes to divide total number of specs. It will print the metrics showing number of tests run per process as given below.

2 processes for 118 specs, ~ 59 specs per process

Update CircleCI config.yml for parllelization

Create a temporary rspec directory to store output of the tests run. Run specs as explained in the step above.

run_tests: &run_tests
    name: Run tests
    command: |
      mkdir ~/rspec
      bundle exec rake parallel:spec"[8]"

Add RspecJunitFormatter gem

We use RspecJunitFormatter to generate XML output of specs run.

gem 'rspec_junit_formatter'

Add gem to the Gemfile and perform bundle install if not done already.

The XML output generate by RspecJunitFormatter is compatible with CirlceCI data collection for tests. It can be used with store_test_results step of CircleCI configuration.

Configure .rspec or .rspec_parallel

Update .rspec or .rspec_parallel stating formatter to be used, XML files with test env number based on parallel processes run and output directory for rspec test output as given below.

--require spec_helper
--format documentation
--format progress --format RspecJunitFormatter --out ~/rspec/rspec<%= ENV["TEST_ENV_NUMBER"] %>.xml

Update CircleCI config.yml to store test results

Add a step in your build job like below.

  - store_test_results:
      path: tmp/rspec

This will upload XML output files generated in the step above, to CircleCI and it will start generating and showing passed / failed test metrics on It’s UI.


Akshay Mohite

Hi there! I am a Ruby on Rails & ReactJS Enthusiast, building some cool products at DTree Labs.

Read More
Buy me a coffee