RSpec: Debug flickering test suites with rspec --bisect
In modern default RSpec configurations, your tests are usually run in random order. This helps to detect "flickering" tests that only fail when run in a certain order.
The reason for this are tests that have side effects causing other tests to fail later. The hard part is to find the offending test.
- Say you have a flickering test that passes on its own, but you just saw it fail in a full test run. At the top of the RSpec output, you will see a message like
Randomized with seed 12345. Take a note of the number. If this message is missing, your tests are not randomized. You can still use this method, simply skip the
Check if the problem is reproducible by runningCopy
rspec --seed 12345
If the failure does not occur again, your test might instead have some random component to it.
rspec --bisectcannot help you with that.
rspec --bisect --seed 12345
This will take some time (maybe about 2x the usual run duration). If successful, RSpec should give you a minimal failing test suite at the end, like this:Copy
The minimal reproduction command is: rspec './spec/other_spec:20' './spec/flickering_spec:30' --seed 12345
With this info you should be able to find the issue much quicker.