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
Now, install the gem to use it with your testing framework.
Run specs in parallel
Now, you can run specs in parallel using following command.
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.
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.
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.
Add RspecJunitFormatter gem
We use RspecJunitFormatter to generate XML output of specs run.
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.
Update CircleCI config.yml to store test results
Add a step in your build job like below.
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.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox