Parallel RSpec Tests on CircleCI With JUnit XML Reports
How to run parallel RSpec tests on CircleCI with JUnit XML reports auto merged to show results of tests in CircleCI web UI.
Join the DZone community and get the full member experience.Join For Free
You will learn how to run RSpec tests for your Ruby on Rails project on CircleCI with parallel jobs to shorten the running time of your CI build. Moreover, you will learn how to configure JUnit formatter to generate an XML report for your tests to show failing RSpec test examples nicely in CircleCI web UI. Finally, you will see how to automatically detect slow spec files and divide their test examples between parallel jobs to eliminate the bottleneck job that’s taking too much time to run tests.
Ruby Gems to Configure Your RoR Project
Here are the key elements you need:
- rspec_junit_formatter - it’s a ruby gem that generates an XML report for executed tests with information about test failures. This report can be automatically read by CircleCI to present it in CircleCI web UI. No more browsing through long RSpec output - just look at highlighted failing specs in the
- knapsack_pro- it’s a Ruby gem for running tests on parallel CI jobs to ensure all jobs finish work at a similar time to save you as much time as possible and eliminate bottlenecks.
- It uses the Queue Mode to dynamically split test files between parallel jobs.
- Knapsack Pro can also detect your slow RSpec test files and divide them between parallel jobs by test examples. You don’t have to manually split your big spec file into smaller files if you want to split work between parallel container on CircleCI :)
Just add the above gems to your
If you use
knapsack_pro gem in Queue Mode with CircleCI you may want to collect metadata like JUnit XML report about your RSpec test suite. The important step for CircleCI is to copy the XML report to
$CIRCLE_TEST_REPORTS directory. Below is a full config for your
spec_helper.rb file (source code from FAQ):
You need the above logic in place to move the XML report from one place to another to avoid accidentally corrupting your XML file. When Knapsack Pro in Queue Mode runs your tests then it fetches a set of test files from Knapsack Pro Queue API and runs it and generates the XML report. After that, another set of test files is fetched from Queue API and the XML report is updated on the disk. If the report already exists on the disk it can get corrupted due to overriding the same file. That’s why you need to move the file to a different location after each set of tests from Queue API is executed.
CircleCI YML Configuration for RSpec
Here is the complete CircleCI YML config file for RSpec, Knapsack Pro and JUnit formatter.
You’ve just learned how to make your CircleCI builds way faster! Now your RSpec tests can be automatically run on many parallel machines to save you time. Please let us know if it was helpful or if you have any questions. Feel free to sign up at Knapsack Pro or down below and try it yourself.
Published at DZone with permission of Artur Trzop. See the original article here.
Opinions expressed by DZone contributors are their own.