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

Commit 5ffb939

Browse files
committed
Add fail_if_no_examples option: better tests
1 parent 322dec2 commit 5ffb939

File tree

5 files changed

+120
-91
lines changed

5 files changed

+120
-91
lines changed

features/.nav

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- read_options_from_file.feature
4444
- color.feature
4545
- fail_fast.feature
46+
- fail_if_no_examples.feature
4647
- custom_settings.feature
4748
- alias_example_to.feature
4849
- default_path.feature
Lines changed: 27 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,46 @@
11
Feature: fail if no examples
22

3-
Use the `fail_if_no_examples` option to make RSpec return non zero exit status
4-
when there are 0 examples.
5-
6-
Scenario: A failing spec example and `fail_if_no_examples` set to true
3+
Use the `fail_if_no_examples` option to make RSpec exit with exit status 1
4+
if there are 0 examples. Using this option, it is recommended to add:
5+
a `--require spec_helper` option to `.rspec` file to ensure the `fail_if_no_examples`
6+
option is set even if no spec files are loaded.
7+
8+
This option may be particularly useful when you happen to not run RSpec tests
9+
locally, but rely on CI to do this. This prevents from false positive builds,
10+
when you expected some RSpec examples to be run, but none were run. Such
11+
a situation may be caused by your misconfiguration or regression/major changes
12+
in RSpec.
13+
14+
Background:
715
Given a file named "spec/spec_helper.rb" with:
816
"""ruby
917
RSpec.configure { |c| c.fail_if_no_examples = true }
1018
"""
11-
Given a file named "spec/example_spec.rb" with:
12-
"""ruby
13-
require 'spec_helper'
14-
15-
RSpec.describe "something" do
16-
it "fails" do
17-
fail
18-
end
19-
end
20-
"""
21-
When I run `rspec spec/example_spec.rb`
22-
Then the exit status should be 1
23-
24-
Scenario: A failing spec example and `fail_if_no_examples` set to false
25-
Given a file named "spec/spec_helper.rb" with:
26-
"""ruby
27-
RSpec.configure { |c| c.fail_if_no_examples = false }
28-
"""
29-
Given a file named "spec/example_spec.rb" with:
19+
Given a file named ".rspec" with:
3020
"""ruby
31-
require 'spec_helper'
32-
33-
RSpec.describe "something" do
34-
it "fails" do
35-
fail
36-
end
37-
end
38-
"""
39-
When I run `rspec spec/example_spec.rb`
40-
Then the exit status should be 1
41-
42-
Scenario: A passing spec example and `fail_if_no_examples` set to true
43-
Given a file named "spec/spec_helper.rb" with:
44-
"""ruby
45-
RSpec.configure { |c| c.fail_if_no_examples = true }
21+
--require spec_helper
4622
"""
47-
Given a file named "spec/example_spec.rb" with:
23+
Given a file named "spec/some.spec.rb" with:
4824
"""ruby
49-
require 'spec_helper'
50-
51-
RSpec.describe "something" do
52-
it "succeeds" do
25+
RSpec.describe 'something' do
26+
it 'succeeds' do
5327
true
5428
end
5529
end
5630
"""
57-
When I run `rspec spec/example_spec.rb`
58-
Then the exit status should be 0
5931

60-
Scenario: A passing spec example and `fail_if_no_examples` set to false
61-
Given a file named "spec/spec_helper.rb" with:
62-
"""ruby
63-
RSpec.configure { |c| c.fail_if_no_examples = false }
64-
"""
65-
Given a file named "spec/example_spec.rb" with:
66-
"""ruby
67-
require 'spec_helper'
32+
Scenario: Examples file has invalid name, thus there are no examples run
33+
When I run `rspec`
34+
Then it should fail with "0 examples, 0 failures"
6835

69-
RSpec.describe "something" do
70-
it "succeeds" do
36+
Scenario: Examples file has valid name and 1 example is run
37+
Given a file named "spec/some_spec.rb" with:
38+
"""ruby
39+
RSpec.describe 'something' do
40+
it 'succeeds' do
7141
true
7242
end
7343
end
7444
"""
75-
When I run `rspec spec/example_spec.rb`
76-
Then the exit status should be 0
77-
78-
Scenario: No examples and `fail_if_no_examples` set to true
79-
Given a file named "spec/spec_helper.rb" with:
80-
"""ruby
81-
RSpec.configure { |c| c.fail_if_no_examples = true }
82-
"""
83-
Given a file named "spec/example_spec.rb" with:
84-
"""ruby
85-
require 'spec_helper'
86-
87-
RSpec.describe "something" do
88-
end
89-
"""
90-
When I run `rspec spec/example_spec.rb`
91-
Then the exit status should be 1
92-
93-
Scenario: No examples and `fail_if_no_examples` set to false
94-
Given a file named "spec/spec_helper.rb" with:
95-
"""ruby
96-
RSpec.configure { |c| c.fail_if_no_examples = false }
97-
"""
98-
Given a file named "spec/example_spec.rb" with:
99-
"""ruby
100-
require 'spec_helper'
101-
102-
RSpec.describe "something" do
103-
end
104-
"""
105-
When I run `rspec spec/example_spec.rb`
106-
Then the exit status should be 0
45+
When I run `rspec --pattern spec/**/*_spec.rb`
46+
Then it should pass with "1 example, 0 failures"

lib/rspec/core/runner.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ def setup(err, out)
108108
# or the configured failure exit code (1 by default) if specs
109109
# failed.
110110
def run_specs(example_groups)
111-
@configuration.reporter.report(@world.example_count(example_groups)) do |reporter|
111+
examples_count = @world.example_count(example_groups)
112+
@configuration.reporter.report(examples_count) do |reporter|
112113
@configuration.with_suite_hooks do
113-
if @world.example_count(example_groups) == 0 && @configuration.fail_if_no_examples
114+
if examples_count == 0 && @configuration.fail_if_no_examples
114115
return 1
115116
end
116117
example_groups.map { |g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'support/aruba_support'
2+
3+
RSpec.describe 'Fail if no examples' do
4+
include_context "aruba support"
5+
before { clean_current_dir }
6+
7+
context 'when 1 passing example' do
8+
def passing_example(fail_if_no_examples)
9+
"
10+
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} }
11+
12+
RSpec.describe 'something' do
13+
it 'succeeds' do
14+
true
15+
end
16+
end
17+
"
18+
end
19+
20+
it 'succeeds if fail_if_no_examples set to true' do
21+
write_file 'spec/example_spec.rb', passing_example(true)
22+
run_command ""
23+
expect(last_cmd_stdout).to include("1 example, 0 failures")
24+
expect(last_cmd_exitstatus).to eq(0)
25+
end
26+
27+
it 'succeeds if fail_if_no_examples set to false' do
28+
write_file 'spec/example_spec.rb', passing_example(false)
29+
run_command ""
30+
expect(last_cmd_stdout).to include("1 example, 0 failures")
31+
expect(last_cmd_exitstatus).to eq(0)
32+
end
33+
end
34+
35+
context 'when 1 failing example' do
36+
def failing_example(fail_if_no_examples)
37+
"
38+
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} }
39+
40+
RSpec.describe 'something' do
41+
it 'fails' do
42+
fail
43+
end
44+
end
45+
"
46+
end
47+
48+
it 'fails if fail_if_no_examples set to true' do
49+
write_file 'spec/example_spec.rb', failing_example(true)
50+
run_command ""
51+
expect(last_cmd_stdout).to include("1 example, 1 failure")
52+
expect(last_cmd_exitstatus).to eq(1)
53+
end
54+
55+
it 'fails if fail_if_no_examples set to false' do
56+
write_file 'spec/example_spec.rb', failing_example(false)
57+
run_command ""
58+
expect(last_cmd_stdout).to include("1 example, 1 failure")
59+
expect(last_cmd_exitstatus).to eq(1)
60+
end
61+
end
62+
63+
context 'when 0 examples' do
64+
def no_examples(fail_if_no_examples)
65+
"
66+
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} }
67+
68+
RSpec.describe 'something' do
69+
end
70+
"
71+
end
72+
73+
it 'fails if fail_if_no_examples set to true' do
74+
write_file 'spec/example_spec.rb', no_examples(true)
75+
run_command ""
76+
expect(last_cmd_stdout).to include("0 examples, 0 failures")
77+
expect(last_cmd_exitstatus).to eq(1)
78+
end
79+
80+
it 'succeeds if fail_if_no_examples set to false' do
81+
write_file 'spec/example_spec.rb', no_examples(false)
82+
run_command ""
83+
expect(last_cmd_stdout).to include("0 examples, 0 failures")
84+
expect(last_cmd_exitstatus).to eq(0)
85+
end
86+
end
87+
end

spec/support/aruba_support.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module ArubaLoader
1010
let(:stderr) { StringIO.new }
1111
let(:stdout) { StringIO.new }
1212

13-
attr_reader :last_cmd_stdout, :last_cmd_stderr
13+
attr_reader :last_cmd_stdout, :last_cmd_stderr, :last_cmd_exitstatus
1414

1515
def run_command(cmd)
1616
RSpec.configuration.color = true
@@ -24,7 +24,7 @@ def run_command(cmd)
2424

2525
handle_current_dir_change do
2626
in_current_dir do
27-
RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
27+
@last_cmd_exitstatus = RSpec::Core::Runner.run(cmd_parts, temp_stderr, temp_stdout)
2828
end
2929
end
3030
ensure

0 commit comments

Comments
 (0)