minitest-bisect

home

github.com/seattlerb/minitest-bisect

rdoc

docs.seattlerb.org/minitest-bisect

DESCRIPTION:

Hunting down random test failures can be very very difficult, sometimes impossible, but minitest-bisect makes it easy.

minitest-bisect helps you isolate and debug random test failures.

If your tests only fail randomly, you can reproduce the error consistently by using `–seed <num>`, but what then? How do you figure out which combination of tests out of hundreds are responsible for the failure? You know which test is failing, but what others are causing it to fail or were helping it succeed in a different order? That's what minitest-bisect does best.

FEATURES/PROBLEMS:

minitest_bisect first runs your tests on a per-file basis to
minimize the number of tests you need to sift through.
minitest_bisect next runs the minimized files and figures out your
exact failure reproduction.

SYNOPSIS:

Let's say you have a bunch of test files and they fail sometimes, but not consistently. You get a run that fails, so you record the randomization seed that minitest displays at the top of every run.

Original Failure:

Here's an example run that fails randomly:

$ rake
ruby -I.:lib -e 'require "example/test_bad1.rb";require "example/test_bad2.rb";require "example/test_bad3.rb";require "example/test_bad4.rb";require "example/test_bad5.rb";require "example/test_bad6.rb";require "example/test_bad7.rb";require "example/test_bad8.rb"'
Run options: --seed 3911

# Running:

..............................................................................
..............................................................................
..............................................................................
..............................................................................
.........................................F....................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
....................

Finished in 200.836561s, 3.9833 runs/s, 3.9784 assertions/s.

  1) Failure:
TestBad4#test_bad4_4 [example/helper.rb:16]:
muahahaha order dependency bug!

800 runs, 799 assertions, 1 failures, 0 errors, 0 skips

Minimization and Isolation:

The problem is about how to efficiently figure out the minimal reproduction of the random failure so you can actually focus on the problem and not all the noise.

So, you run the tests again, but this time with minitest_bisect. Provide the seed given in the failure so the tests always run in the same order and reproduce every time.

minitest_bisect will first minimize the number of files, then it will turn around and minimize the number of methods.

$ minitest_bisect --seed 3911 example/test*.rb
reproducing...
reproduced
# of culprit files: 4
# of culprit files: 2
# of culprit files: 2
# of culprit files: 2
# of culprit files: 2

Minimal files found in 5 steps:

ruby -Ilib -e 'require "./example/test_bad1.rb" ; require "./example/test_bad4.rb"' -- --seed 3911 -s 48222

reproducing...
reproduced
# of culprit methods: 64
# of culprit methods: 64
# of culprit methods: 32
# of culprit methods: 16
# of culprit methods: 8
# of culprit methods: 8
# of culprit methods: 4
# of culprit methods: 2
# of culprit methods: 2
# of culprit methods: 1

Minimal methods found in 10 steps:

ruby -Ilib -e 'require "./example/test_bad1.rb" ; require "./example/test_bad4.rb"' -- --seed 3911 -s 48222 -n '/^(?:TestBad1\#test_bad1_1|TestBad4\#test_bad4_4)$/'

Final reproduction:

Run options: --seed 3911 -s 48222 -n "/^(?:TestBad1\\#test_bad1_1|TestBad4\\#test_bad4_4)$/"

# Running:

.F

Finished in 0.505776s, 3.9543 runs/s, 1.9772 assertions/s.

  1) Failure:
TestBad4#test_bad4_4 [/Users/ryan/Work/p4/zss/src/minitest-bisect/dev/example/helper.rb:16]:
muahahaha order dependency bug!

2 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Voila! This reduced it from 800 tests across 8 files down to 2 tests across 2 files. Note how we went from a 200 second test run to a 0.5 second test run. Debugging that will be much easier.

It is now up to you to look at the source of both of those tests to determine what side-effects (or lack thereof) are causing your test failure when run in this specific order.

This happens in a single run. Depending on how many files / tests you have and how long they take, the minimization might take a long time, but each iteration can reduce the number of tests so it should get quicker with time.

REQUIREMENTS:

minitest 5

INSTALL:

sudo gem install minitest-bisect

FAQ:

Q: What if my tests are in multiple subdirectories?

This is assuming the following doesn't work for you:

minitest_bisect --seed 3911 test  _test.rb

because you need it to span multiple sub-directories.

A: Use your shell to find the files for you

minitest_bisect --seed 3911 $(find test -type f -name \*_test.rb)

or

minitest_bisect --seed 3911 test/   *_test.rb

Q: How do I resolve an “invalid option: --server” error?

$ MTB_VERBOSE=2 minitest_bisect --seed 3911 example/test*.rb
reproducing...
ruby -Ilib -e 'require "./example/test_bad1.rb" ; require "./example/test_bad4.rb"' -- --seed 3911 --server 33352

invalid option: --server

minitest options:
    -h, --help                       Display this help.
    -s, --seed SEED                  Sets random seed. Also via env. Eg: SEED=n rake
    -v, --verbose                    Verbose. Show progress processing files.
    -n, --name PATTERN               Filter run on /regexp/ or string.

Known extensions: guard_minitest, pride
    -p, --pride                      Pride. Show your testing pride!
Reproduction run passed? Aborting. Try running with MTB_VERBOSE=2 to verify.

A: Try adding “gem minitest-bisect” to your Gemfile and running “bundle install

LICENSE:

(The MIT License)

Copyright © Ryan Davis, seattle.rb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.