A randomly failing test is one of the most annoying moments during test creation process. Mostly, such spec doesn’t fail in our development environment but on Continuous Integration service. If you are not familiarized with CI yet then you can check Wikipedia page or services like Travis CI or Circle CI.

Debugging randomly failing specs may be a time-consuming process. The first step is to find the right order of executing specs to reproduce our issue and the second step is to fix the issue. RSpec would not help us with the second step but it can help with finding the right order to reproduce failing scenario. In order to do this, use --bisect flag:

bundle exec rspec spec/controllers --bisect

This flag is available from 3.3 version.

Your output should be similar:

Bisect started using options: "spec/controllers"
Running suite to find failures... (3.37 seconds)
Starting bisect with 2 failing examples and 5 non-failing examples.
Checking that failure(s) are order-dependent... failure(s) do not require any non-failures to run first

Bisect complete! Reduced necessary non-failing examples from 5 to 0 in 3.25 seconds.

The minimal reproduction command is:
  rspec ./spec/controllers/contacts_controller_spec.rb[1:2:2,1:2:3]

The output will give you an instruction which command you should use to reproduce randomly failing specs. Now it’s your job to fix the issue.

Download free RSpec & TDD ebook

Do you want to earn more or jump to the next level in your company? Do you know that testing skills are one of the most desired skills? There is only first step: start testing and do it right. My ebook can help you. Subscribe to the newsletter to get a free copy of the book.

One Comment

  1. Thanks! It help me to fix a random fail test today.

Leave a Comment

Your email address will not be published. Required fields are marked *