diff --git a/README.md b/README.md index a18b949..9c68be4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1437 TILs and counting..._ +_1438 TILs and counting..._ --- @@ -1101,6 +1101,7 @@ _1437 TILs and counting..._ - [Avoid Accidentally Disabling Pry](rspec/avoid-accidentally-disabling-pry.md) - [Check Specific Arguments To Received Method](rspec/check-specific-arguments-to-received-method.md) +- [Configure Tests To Run In Random Order](rspec/configure-tests-to-run-in-random-order.md) - [Find Minimal Set Of Tests Causing A Flicker](rspec/find-minimal-set-of-tests-causing-a-flicker.md) - [Format Test Results As A JSON File](rspec/format-test-results-as-a-json-file.md) - [Run Tests With Documentation Formatting](rspec/run-tests-with-documentation-formatting.md) diff --git a/rspec/configure-tests-to-run-in-random-order.md b/rspec/configure-tests-to-run-in-random-order.md new file mode 100644 index 0000000..874ae6c --- /dev/null +++ b/rspec/configure-tests-to-run-in-random-order.md @@ -0,0 +1,33 @@ +# Configure Tests To Run In Random Order + +By default, an RSpec test suite is going to run in a predictable, sequential +order, every time. + +When testing the parts of a complex Rails app that have all kinds of test data +that needs to be set up, I prefer to have my tests always run in a random +(repeatable with a seed) order. This way I'm more likely to catch sooner, +rather than later, bugs that are hidden by passing tests due to test data setup +that happens to work in a specific order. + +RSpec can be configured to run tests in a random, seedable order in the +`spec_helper.rb` file. + +```ruby +RSpec.configure do |config| + config.order = :random +end +``` + +Whenever you run your test suite, the first thing you'll see is a message like +this: + +``` +Randomized with seed 7011 +``` + +That seed number can be used to re-run the suite in a repeatable order when you +need to do so to track down an order-dependent failing test. + +```bash +$ be rspec --seed 7011 +```