r/ruby • u/petrenkorf • May 25 '26
How did you solved your issues regarding slow test suites?
The application I am currently working on contains something around 25k spec files. It is impossible to run the test suite on a single developer machine, because we always hit a point where the process is killed. For CI we use multiple machines running rspec in parallel, and yet, it takes almost 15 minutes on each instace to finish testing the application.
Have you faced this kind of scenario before? How did you solved it?
5
Upvotes
6
u/fglc2 May 25 '26
test-prof has a lot of tools for diagnosing performance problems in tests. Beyond that parallelisation is useful (flatware).
I’ve heard that orgs like shopify use test selection frameworks so that locally only the subset of tests that are likely to be relevant to your changes are run. They describe this and some of their other approaches https://shopify.engineering/faster-shopify-ci
Why do your test processes get killed though - that sounds like a different problem to just “slow”
2
u/armahillo May 26 '26
Things to look into:
Also....
You mean spec examples .... right? If you have 25k spec files, I would definitely start by evaluating if you should have that many files.
4
u/Tolexx May 25 '26
We used Knapsack gem the opensource version to parallelize our test suite on the CI. It was really helpful. I did most of the work and was able to get it down from 40mins to about ~9mins running on 10 parallel nodes.
Before parallelization I did some profiling work using testprof gem. It's really good too as it helped to spot some areas where things were really slow.
1
u/Asmod4n May 27 '26
Hm, this could get solved by only running tests against contracts that have code changes.
Would need some form of book keeping which contracts you have defined and how they interact with each other.
I guess there is no such thing out there so someone would have to build it.
2
u/edwardfingerhands May 25 '26
set it up so each dev can run 'personal' builds on CI infrastructure. Parallelise the CI infrastructure so that it can run a full build in < 10 minutes. Horizontally scale the CI infrastructure so that multiple devs can run builds at once.
2
u/samgranieri May 25 '26
I’m using parallel specs. It’s possible to run the entire suite on your laptop in parallel, but you need a beefy laptop to pull it off.
I’m starting to regret using factory bot. Maybe DHH was right and using fixtures would have been the better approach. I’m coming at this with 20 years of experience with Rails.
2
u/poop-machine May 25 '26
Realistically, you'll never be able to run an enterprise-grade product test suite on a single dev machine in a reasonable amount of time. Your best bet (apart from profiling bottlenecks, stubbing out expensive calls, and using parallelization) is to identify and extract foundational components of your codebase that rarely change and don't need to be retested on every run. Re-package them as individual private gems with their own test suites.
2
u/OlivarTheLagomorph May 25 '26
2
u/TommyTheTiger May 28 '26
Well, you limit the test files you run locally as a start. This sounds suspiciously like large rails app. You using let_it_be already? Keeping tests that talk to the db fast means imo being keenly aware of how and when you are talking to it, and keeping the chatter down.
2
u/lautan May 25 '26
Use mini test via rails test. It supports running tests in multiple processes. And fixtures.
1
u/Weird_Suggestion May 25 '26
I can only speak what I do locally. I’ve been using retest every day for over 5 years. Note I maintain retest lol.
I use it either to run a subset of specs against the diffs from a commit/branch, run an exact subset of specs on every file changes or run unit tests on any file change.
It doesn’t analyse code path execution because that often needs a successful run of your suite to start with. It’s a simple runner that will follow naming conventions to run specs on file changes. It’s good enough as a sane check before pushing and triggering CI. CI will still fail from unrelated specs every now and then but that’s marginal in my situation. It’s very capable and people find it useful but I can imagine it’s easy to discard thinking it won’t fit somehow.
Things are changing in that space too. I’d imagine AI could possibly do a good enough job to identify which subset to run. Retest might not be needed in the future but I still find it useful as part of my workflow since we’re not full agent loopers at work.
It’s exciting times for testing I think.
1
u/growlybeard May 26 '26
I recently refactored my tests for performance
Biggest wins are:
I also vibe coded a patch to Rspec and rspec rails to enable parallel specs without a gem, but that has the same performance as parallel tests
The Minitest is faster than Rspec stuff is due to:
For plain Ruby, without parallelism, Minitest is like 1% faster. So if you like Rspec, stick with it. Just install gem for parallelism and start using fixtures.
FixtureBot gem aims to let you keep factory bot semantics while using fixtures.