Fortunately thanks to very smart people in the community - yes there is! So worry not, warrior of legacy code, protector of good practices. Now you have tools that will help you lead the way into the clean and solid code, where tests are always green and developers are happy. Ok, maybe I got carried away a little bit here, let’s get down to the business ;).
Rspec 3.3 introduced bisect command which is truly awesome, from docs:
Pass the –bisect option (in addition to –seed and any other options) and RSpec will repeatedly run subsets of your suite to isolate the minimal set of examples that reproduce the same failures.
I recommend checkout out this pull request for more references regarding internals/implementation.
Side note: still on Rspec 2.x? Checkout transpec that will help you with migration to 3.x.
So let’s say you have some sort of test suite that is passing. Unless you add config.order = :random
to your rspec config (or run it with --order rand
flag), then it’s not. Test suite should fail with a seed of some sort (eg. Randomized with seed 45379), pass that seed to bisect command (rspec --seed 45379 --bisect
) and you should end up with a very narrow subset of tests that should help you a lot with the process of investigating what went wrong - of course, this often won’t solve everything magically, but should help you guide into the right place.
Random tips / notes:
depending on test suite size - it might take some time to compute, I suggest disabling Rails logging (via
Rails.logger.level = 4
) for some free performance gain and keeping database in memory if possible - if you’re using docker you can mount database directory as tmpfsif you’re having still troubles or rspec can’t bisect your test suite try with different seed
generally, watch out for
before
/after
-(:all)
hooks, unclear/magical fixtures that might mess up global database state (and other code that touches globals/opens classes)additionally, you can run each spec individually which might be a good approach in some cases
find spec -name '*_spec.rb' -exec rspec {} \;
(you might want to quote '{}'
under fish shell)
Good luck! ⚔️