OOZOU
Get in Touch
Back to Blog

Running Specs in Parallel before Rails 6

The Rails 6 release included parallelized tests, but did you know you can already do this? We've been using parallel_tests to run our RSpec tests in parallel in our GitHub action for months.

December 29, 2020
3 min read
Running Specs in Parallel before Rails 6

As a Rails developer you'll be running a lot of specs. We often use guard to automate the process during development or use --only-failures to quickly fix a few failing specs. However, in our CI flows we need to run the full suite to ensure we're green before merging or deploying. In all of our Rails projects we use GitHub Actions to run all of our CI checks. We typically run Pronto to check if our code is up to Rubocop standards and RSpec to ensure we haven't broken anything. We run these actions for every push to a Pull Request and every merge to our develop and master branches. For new projects this is typically a pretty fast process, maybe a couple of minutes, however, some of our older projects might take upwards of 10 minutes to run the full test suite. Last year I joined one of our longest running projects and immediately tasked myself with reducing the duration of our test suite. When I joined, the suite would take 19+ minutes to run. The first thing I did was run the suite with the profile flag resulting in the 10 slowest examples. Any that we're taking more than a couple of seconds got some attention to see if I could reduce them. Most of these specs used a lot of setup and teardown logic so I switched to stubbing which dramatically reduced a lot of the run times of these examples. At the same time, a colleague switched our mechanism for matching json responses which reduced our duration even further. We managed to get the suite down to around 8 minutes. A really impressive win. I had one last trick up my sleeve. Parallel Tests!

How Do Parallel Tests Work?

The logic is pretty simple. Most computers have more than one CPU core but when we run RSpec we only use one of them. Parallel Tests checks how many cores we have available on the current machine and then splits the test suite up into that many groups. Each group is run in its own core with its own database and other processes to isolate the tests to their own core. parallel_tests will expose an ENV var to let your code know which test group is currently running so you can modify the logic for starting services based on this variable. e.g. if we use PORT 9250 for elasticsearch in our specs we can change this to:

9250 + ENV['TEST_ENV_NUMBER'].to_i

We also need to do the same for our database name:

test:
  database: hello_world<%= ENV['TEST_ENV_NUMBER'] %>

Now if we run RSpec manually our database will be hello_world but if we run it using parallel_tests we'll have multiple databases e.g. hello_world, hello_world2, hello_world3, hello_world4 Before we run our tests we need to set up these databases with the task parallel_tests provides.

rake parallel:setup

To run our specs in parallel we simply change the invocation from rspec to parallel_tests

# bundle exec rspec spec
rake parallel:spec

That's it! Now we're running our specs in parallel pre-Rails-6. With this simple change our 8 minute test suite went down to 4 minutes on GitHub which uses 2 core machines to run their actions. On 4 core machines we can run the specs in 2-3 minutes. That's a massive improvement over the original 19+ minutes we had before.

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Whitepapers
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Get an Estimate
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy