Skip to content

Commit df3492d

Browse files
committed
Add Configure Tests To Run In Random Order as an RSpec TIL
1 parent bd49b31 commit df3492d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1437 TILs and counting..._
13+
_1438 TILs and counting..._
1414

1515
---
1616

@@ -1101,6 +1101,7 @@ _1437 TILs and counting..._
11011101

11021102
- [Avoid Accidentally Disabling Pry](rspec/avoid-accidentally-disabling-pry.md)
11031103
- [Check Specific Arguments To Received Method](rspec/check-specific-arguments-to-received-method.md)
1104+
- [Configure Tests To Run In Random Order](rspec/configure-tests-to-run-in-random-order.md)
11041105
- [Find Minimal Set Of Tests Causing A Flicker](rspec/find-minimal-set-of-tests-causing-a-flicker.md)
11051106
- [Format Test Results As A JSON File](rspec/format-test-results-as-a-json-file.md)
11061107
- [Run Tests With Documentation Formatting](rspec/run-tests-with-documentation-formatting.md)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Configure Tests To Run In Random Order
2+
3+
By default, an RSpec test suite is going to run in a predictable, sequential
4+
order, every time.
5+
6+
When testing the parts of a complex Rails app that have all kinds of test data
7+
that needs to be set up, I prefer to have my tests always run in a random
8+
(repeatable with a seed) order. This way I'm more likely to catch sooner,
9+
rather than later, bugs that are hidden by passing tests due to test data setup
10+
that happens to work in a specific order.
11+
12+
RSpec can be configured to run tests in a random, seedable order in the
13+
`spec_helper.rb` file.
14+
15+
```ruby
16+
RSpec.configure do |config|
17+
config.order = :random
18+
end
19+
```
20+
21+
Whenever you run your test suite, the first thing you'll see is a message like
22+
this:
23+
24+
```
25+
Randomized with seed 7011
26+
```
27+
28+
That seed number can be used to re-run the suite in a repeatable order when you
29+
need to do so to track down an order-dependent failing test.
30+
31+
```bash
32+
$ be rspec --seed 7011
33+
```

0 commit comments

Comments
 (0)