Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 32d5821

Browse files
authored
Merge pull request #2929 from santib/rspec-random
Use random order by default
2 parents e2b3402 + 62671c9 commit 32d5821

24 files changed

+81
-38
lines changed

features/command_line/fail_fast.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ Feature: `--fail-fast` option
3030
"""
3131

3232
Scenario: Using `--fail-fast`
33-
When I run `rspec . --fail-fast`
33+
When I run `rspec . --fail-fast --order defined`
3434
Then the output should contain ".F"
3535
Then the output should not contain ".F."
3636

3737
Scenario: Using `--fail-fast=3`
38-
When I run `rspec . --fail-fast=3`
38+
When I run `rspec . --fail-fast=3 --order defined`
3939
Then the output should contain ".FFF"
4040
Then the output should not contain ".FFFF."
4141

4242
Scenario: Using `--no-fail-fast`
43-
When I run `rspec . --no-fail-fast`
43+
When I run `rspec . --no-fail-fast --order defined`
4444
Then the output should contain ".FFFF."

features/command_line/format_option.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ Feature: `--format` option
4848
"""
4949

5050
Scenario: Progress bar format (default)
51-
When I run `rspec --format progress example_spec.rb`
51+
When I run `rspec --format progress example_spec.rb --order defined`
5252
Then the output should contain ".F**"
5353

5454
Scenario: Documentation format
55-
When I run `rspec example_spec.rb --format documentation`
55+
When I run `rspec example_spec.rb --format documentation --order defined`
5656
Then the output should contain:
5757
"""
5858
something
@@ -63,7 +63,7 @@ Feature: `--format` option
6363
"""
6464

6565
Scenario: Documentation format saved to a file
66-
When I run `rspec example_spec.rb --format documentation --out rspec.txt`
66+
When I run `rspec example_spec.rb --format documentation --out rspec.txt --order defined`
6767
Then the file "rspec.txt" should contain:
6868
"""
6969
something
@@ -74,7 +74,7 @@ Feature: `--format` option
7474
"""
7575

7676
Scenario: Multiple formats and output targets
77-
When I run `rspec example_spec.rb --format progress --format documentation --out rspec.txt`
77+
When I run `rspec example_spec.rb --format progress --format documentation --out rspec.txt --order defined`
7878
Then the output should contain ".F**"
7979
And the file "rspec.txt" should contain:
8080
"""

features/configuration/custom_settings.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Feature: custom settings
2525
end
2626
end
2727
"""
28-
When I run `rspec ./additional_setting_spec.rb`
28+
When I run `rspec ./additional_setting_spec.rb --order defined`
2929
Then the examples should all pass
3030

3131
Scenario: Default to `true`
@@ -51,7 +51,7 @@ Feature: custom settings
5151
end
5252
end
5353
"""
54-
When I run `rspec ./additional_setting_spec.rb`
54+
When I run `rspec ./additional_setting_spec.rb --order defined`
5555
Then the examples should all pass
5656

5757
Scenario: Overridden in a subsequent `RSpec.configure` block

features/configuration/fail_fast.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Feature: fail fast
3737
end
3838
end
3939
"""
40-
When I run `rspec spec/example_spec.rb -fd`
40+
When I run `rspec spec/example_spec.rb -fd --order defined`
4141
Then the output should contain "1 example, 1 failure"
4242

4343
Scenario: `fail_fast` with multiple files, second example failing (only runs the first two examples)
@@ -77,7 +77,7 @@ Feature: fail fast
7777
end
7878
end
7979
"""
80-
When I run `rspec spec`
80+
When I run `rspec spec --order defined`
8181
Then the output should contain "2 examples, 1 failure"
8282

8383

@@ -105,5 +105,5 @@ Feature: fail fast
105105
end
106106
end
107107
"""
108-
When I run `rspec spec/example_spec.rb -fd`
108+
When I run `rspec spec/example_spec.rb -fd --order defined`
109109
Then the output should contain "3 examples, 2 failures"

features/example_groups/aliasing.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Feature: aliasing
3636
end
3737
end
3838
"""
39-
When I run `rspec nested_example_group_aliases_spec.rb --tag detailed -fdoc`
39+
When I run `rspec nested_example_group_aliases_spec.rb --tag detailed -fdoc --order defined`
4040
Then the output should contain:
4141
"""
4242
a detail

features/example_groups/basic_structure.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Feature: basic structure (`describe`/`it`)
4545
end
4646
end
4747
"""
48-
When I run `rspec nested_example_groups_spec.rb -fdoc`
48+
When I run `rspec nested_example_groups_spec.rb -fdoc --order defined`
4949
Then the output should contain:
5050
"""
5151
something

features/example_groups/shared_examples.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Feature: shared examples
124124
it_behaves_like "a collection"
125125
end
126126
"""
127-
When I run `rspec collection_spec.rb --format documentation`
127+
When I run `rspec collection_spec.rb --format documentation --order defined`
128128
Then the examples should all pass
129129
And the output should contain:
130130
"""
@@ -176,7 +176,7 @@ Feature: shared examples
176176
end
177177
end
178178
"""
179-
When I run `rspec shared_example_group_spec.rb --format documentation`
179+
When I run `rspec shared_example_group_spec.rb --format documentation --order defined`
180180
Then the examples should all pass
181181
And the output should contain:
182182
"""
@@ -212,7 +212,7 @@ Feature: shared examples
212212
it_behaves_like "a measurable object", 6, [:size, :length]
213213
end
214214
"""
215-
When I run `rspec shared_example_group_params_spec.rb --format documentation`
215+
When I run `rspec shared_example_group_params_spec.rb --format documentation --order defined`
216216
Then the examples should all pass
217217
And the output should contain:
218218
"""

features/helper_methods/let.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Feature: let and let!
2727
end
2828
end
2929
"""
30-
When I run `rspec let_spec.rb`
30+
When I run `rspec let_spec.rb --order defined`
3131
Then the examples should all pass
3232

3333
Scenario: Use `let!` to define a memoized helper method that is called in a `before` hook

features/hooks/before_and_after_hooks.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Feature: `before` and `after` hooks
103103
end
104104
end
105105
"""
106-
When I run `rspec before_context_spec.rb`
106+
When I run `rspec before_context_spec.rb --order defined`
107107
Then the examples should all pass
108108

109109
When I run `rspec before_context_spec.rb:15`

features/hooks/filtering.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Feature: filters
228228
it("", :around_example) { puts "example 4" }
229229
end
230230
"""
231-
When I run `rspec --format progress less_verbose_metadata_filter.rb`
231+
When I run `rspec --format progress less_verbose_metadata_filter.rb --order defined`
232232
Then the examples should all pass
233233
And the output should contain:
234234
"""

features/hooks/when_first_matching_example_defined.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Feature: `when_first_matching_example_defined` hook
5353
"""
5454

5555
Scenario: Running the entire suite loads the DB setup
56-
When I run `rspec`
56+
When I run `rspec --order defined`
5757
Then it should pass with:
5858
"""
5959
Bootstrapped the DB.

features/pending_and_skipped_examples/skipped_examples.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Feature: `skip` examples
7171
end
7272
end
7373
"""
74-
When I run `rspec temporarily_skipped_spec.rb`
74+
When I run `rspec temporarily_skipped_spec.rb --order defined`
7575
Then the exit status should be 0
7676
And the output should contain "3 examples, 0 failures, 3 pending"
7777
And the output should contain:

features/subject/explicit_subject.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Feature: Explicit Subject
7373
end
7474
end
7575
"""
76-
When I run `rspec memoized_subject_spec.rb`
76+
When I run `rspec memoized_subject_spec.rb --order defined`
7777
Then the examples should all pass
7878

7979
Scenario: The `subject` is available in `before` hooks
@@ -165,7 +165,7 @@ Feature: Explicit Subject
165165
end
166166
end
167167
"""
168-
When I run `rspec named_subject_spec.rb`
168+
When I run `rspec named_subject_spec.rb --order defined`
169169
Then the examples should all pass
170170

171171
Scenario: Use `subject!(:name)` to define a helper method called before the example

lib/rspec/core/ordering.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ def initialize(configuration)
8585
@configuration = configuration
8686
@strategies = {}
8787

88-
register(:random, Random.new(configuration))
88+
random = Random.new(configuration)
89+
register(:random, random)
8990
register(:recently_modified, RecentlyModified.new)
91+
register(:defined, Identity.new)
9092

91-
identity = Identity.new
92-
register(:defined, identity)
93-
94-
# The default global ordering is --defined.
95-
register(:global, identity)
93+
# The default global ordering is --random.
94+
register(:global, random)
9695
end
9796

9897
def fetch(name, &fallback)

lib/rspec/core/project_initializer/spec/spec_helper.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@
6666
# particularly slow.
6767
config.profile_examples = 10
6868
69-
# Run specs in random order to surface order dependencies. If you find an
70-
# order dependency and want to debug it, you can fix the order by providing
71-
# the seed, which is printed after each run.
72-
# --seed 1234
69+
# RSpec runs specs in a random order by default to surface order dependencies.
70+
# We recommend this setting but it can be changed to others, such as `:defined`
71+
# to run specs in the defined order. See the documentation for more details.
7372
config.order = :random
7473
7574
# Seed global randomization in this process using the `--seed` CLI option.

spec/integration/bisect_runners_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def with_runner(&block)
3939

4040
with_runner do |runner|
4141
expect(runner.original_results).to have_attributes(
42-
:all_example_ids => %w[ ./spec/a_spec.rb[1:1] ./spec/a_spec.rb[1:2] ],
42+
:all_example_ids => match_array(%w[ ./spec/a_spec.rb[1:1] ./spec/a_spec.rb[1:2] ]),
4343
:failed_example_ids => %w[ ./spec/a_spec.rb[1:2] ]
4444
)
4545

spec/integration/suite_hooks_errors_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
c.backtrace_exclusion_patterns << %r{/rspec-core/spec/} << %r{rspec_with_simplecov}
2626
c.failure_exit_code = failure_exit_code
2727
c.error_exit_code = error_exit_code
28+
c.seed = 123
2829
end
2930
end
3031

@@ -50,6 +51,8 @@ def run_spec_expecting_non_zero(before_or_after)
5051
output = run_spec_expecting_non_zero(:before)
5152
expect(output).to eq unindent(<<-EOS)
5253
54+
Randomized with seed 123
55+
5356
An error occurred in a `before(:suite)` hook.
5457
Failure/Error: raise 'boom'
5558
@@ -61,12 +64,16 @@ def run_spec_expecting_non_zero(before_or_after)
6164
Finished in n.nnnn seconds (files took n.nnnn seconds to load)
6265
0 examples, 0 failures, 1 error occurred outside of examples
6366
67+
Randomized with seed 123
68+
6469
EOS
6570
end
6671

6772
it 'nicely formats errors in `after(:suite)` hooks and exits with non-zero' do
6873
output = run_spec_expecting_non_zero(:after)
6974
expect(output).to eq unindent(<<-EOS)
75+
76+
Randomized with seed 123
7077
.
7178
An error occurred in an `after(:suite)` hook.
7279
Failure/Error: raise 'boom'
@@ -79,6 +86,8 @@ def run_spec_expecting_non_zero(before_or_after)
7986
Finished in n.nnnn seconds (files took n.nnnn seconds to load)
8087
1 example, 0 failures, 1 error occurred outside of examples
8188
89+
Randomized with seed 123
90+
8291
EOS
8392
end
8493

@@ -115,6 +124,8 @@ def run_spec_expecting_non_zero(before_or_after)
115124

116125
expect(output).to eq unindent(<<-EOS)
117126
127+
Randomized with seed 123
128+
118129
An error occurred in a `before(:suite)` hook.
119130
Failure/Error: c.before(:suite) { raise 'before 1' }
120131
@@ -140,6 +151,8 @@ def run_spec_expecting_non_zero(before_or_after)
140151
Finished in n.nnnn seconds (files took n.nnnn seconds to load)
141152
0 examples, 0 failures, 3 errors occurred outside of examples
142153
154+
Randomized with seed 123
155+
143156
EOS
144157
end
145158
end

spec/rspec/core/example_group_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@ def define_and_run_group(define_outer_example = false)
823823
end
824824

825825
it "runs before_all_defined_in_config, before all, before each, example, after each, after all, after_all_defined_in_config in that order" do
826+
RSpec.configuration.order = :defined
827+
826828
order = []
827829

828830
RSpec.configure do |c|
@@ -1442,7 +1444,11 @@ def extract_execution_results(group)
14421444
end
14431445

14441446
context "with fail_fast enabled" do
1445-
before { RSpec.configuration.fail_fast = true }
1447+
before do
1448+
RSpec.configuration.fail_fast = true
1449+
RSpec.configuration.order = :defined
1450+
end
1451+
14461452
let(:group) { RSpec.describe }
14471453
let(:reporter) { Reporter.new(RSpec.configuration) }
14481454

@@ -1466,7 +1472,11 @@ def extract_execution_results(group)
14661472
end
14671473

14681474
context "with fail_fast set to 3" do
1469-
before { RSpec.configuration.fail_fast = 3 }
1475+
before do
1476+
RSpec.configuration.fail_fast = 3
1477+
RSpec.configuration.order = :defined
1478+
end
1479+
14701480
let(:group) { RSpec.describe }
14711481
let(:reporter) { Reporter.new(RSpec.configuration) }
14721482

@@ -1628,6 +1638,8 @@ def extract_execution_results(group)
16281638
end
16291639

16301640
it "applies new metadata-based config items based on the update" do
1641+
RSpec.configuration.order = :defined
1642+
16311643
extension = Module.new do
16321644
def extension_method; 17; end
16331645
end
@@ -1674,6 +1686,8 @@ def extension_method; 17; end
16741686
end
16751687

16761688
it "does not cause duplicate hooks to be added when re-configuring the group" do
1689+
RSpec.configuration.order = :defined
1690+
16771691
sequence = []
16781692
RSpec.configure do |c|
16791693
c.before(:example, :foo => true) { sequence << :global_before_hook }

spec/rspec/core/example_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def expect_pending_result(example)
913913
example2 = group.example("example 2") { current_examples << RSpec.current_example }
914914

915915
group.run
916-
expect(current_examples).to eq([example1, example2])
916+
expect(current_examples).to match_array([example1, example2])
917917
end
918918
end
919919

spec/rspec/core/formatters/documentation_formatter_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def execution_result(values)
5252
end
5353

5454
it "represents nested group using hierarchy tree" do
55+
RSpec.configuration.order = :defined
56+
5557
group = RSpec.describe("root")
5658
context1 = group.describe("context 1")
5759
context1.example("nested example 1.1"){}
@@ -112,6 +114,8 @@ def execution_result(values)
112114
end
113115

114116
it "strips whitespace for each row" do
117+
RSpec.configuration.order = :defined
118+
115119
group = RSpec.describe(" root ")
116120
context1 = group.describe(" nested ")
117121
context1.example(" example 1 ") {}

spec/rspec/core/formatters/json_formatter_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
end
2121

2222
it "outputs expected json (brittle high level functional test)" do
23+
RSpec.configuration.order = :defined
24+
2325
its = []
2426
group = RSpec.describe("one apiece") do
2527
its.push it("succeeds") { expect(1).to eq 1 }

0 commit comments

Comments
 (0)