Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exit with a failure status if no RSpec examples. Default behavior not changed. CLI option not implemented.
- Loading branch information
Showing
7 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Feature: fail if no examples | ||
|
||
Use the `fail_if_no_examples` option to make RSpec exit with a failure status (by default 1) if there are no examples. Using this option, it is recommended to add a `--require spec_helper` option to `.rspec` file to ensure the `fail_if_no_examples` option is set even if no spec files are loaded. | ||
|
||
This option may be particularly useful when you happen to not run RSpec tests locally, but rely on CI to do this. This prevents from false positive builds, when you expected some RSpec examples to be run, but none were run. Such a situation may be caused by your misconfiguration or regression/major changes in RSpec. | ||
|
||
Background: | ||
Given a file named "spec/spec_helper.rb" with: | ||
"""ruby | ||
RSpec.configure { |c| c.fail_if_no_examples = true } | ||
""" | ||
Given a file named ".rspec" with: | ||
"""ruby | ||
--require spec_helper | ||
""" | ||
Given a file named "spec/some.spec.rb" with: | ||
"""ruby | ||
RSpec.describe 'something' do | ||
it 'succeeds' do | ||
true | ||
end | ||
end | ||
""" | ||
|
||
Scenario: Examples file name is not matched by RSpec pattern, thus there are no examples run | ||
When I run `rspec` | ||
Then it should fail with "0 examples, 0 failures" | ||
|
||
Scenario: Examples file name is matched by RSpec pattern, 1 example is run | ||
When I run `rspec --pattern spec/**/*.spec.rb` | ||
Then it should pass with "1 example, 0 failures" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
require 'support/aruba_support' | ||
|
||
RSpec.describe 'Fail if no examples' do | ||
include_context "aruba support" | ||
before { clean_current_dir } | ||
|
||
context 'when 1 passing example' do | ||
def passing_example(fail_if_no_examples) | ||
" | ||
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} } | ||
RSpec.describe 'something' do | ||
it 'succeeds' do | ||
true | ||
end | ||
end | ||
" | ||
end | ||
|
||
it 'succeeds if fail_if_no_examples set to true' do | ||
write_file 'spec/example_spec.rb', passing_example(true) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("1 example, 0 failures") | ||
expect(last_cmd_exit_status).to eq(0) | ||
end | ||
|
||
it 'succeeds if fail_if_no_examples set to false' do | ||
write_file 'spec/example_spec.rb', passing_example(false) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("1 example, 0 failures") | ||
expect(last_cmd_exit_status).to eq(0) | ||
end | ||
end | ||
|
||
context 'when 1 failing example' do | ||
def failing_example(fail_if_no_examples) | ||
" | ||
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} } | ||
RSpec.describe 'something' do | ||
it 'fails' do | ||
fail | ||
end | ||
end | ||
" | ||
end | ||
|
||
it 'fails if fail_if_no_examples set to true' do | ||
write_file 'spec/example_spec.rb', failing_example(true) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("1 example, 1 failure") | ||
expect(last_cmd_exit_status).to eq(1) | ||
end | ||
|
||
it 'fails if fail_if_no_examples set to false' do | ||
write_file 'spec/example_spec.rb', failing_example(false) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("1 example, 1 failure") | ||
expect(last_cmd_exit_status).to eq(1) | ||
end | ||
end | ||
|
||
context 'when 0 examples' do | ||
def no_examples(fail_if_no_examples) | ||
" | ||
RSpec.configure { |c| c.fail_if_no_examples = #{fail_if_no_examples} } | ||
RSpec.describe 'something' do | ||
end | ||
" | ||
end | ||
|
||
it 'fails if fail_if_no_examples set to true' do | ||
write_file 'spec/example_spec.rb', no_examples(true) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("0 examples, 0 failures") | ||
expect(last_cmd_exit_status).to eq(1) | ||
end | ||
|
||
it 'succeeds if fail_if_no_examples set to false' do | ||
write_file 'spec/example_spec.rb', no_examples(false) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("0 examples, 0 failures") | ||
expect(last_cmd_exit_status).to eq(0) | ||
end | ||
|
||
context 'when custom failure_exit_code set' do | ||
def no_examples_custom_failure_exit_code(fail_if_no_examples) | ||
" | ||
RSpec.configure do |c| | ||
c.fail_if_no_examples = #{fail_if_no_examples} | ||
c.failure_exit_code = 15 | ||
end | ||
RSpec.describe 'something' do | ||
end | ||
" | ||
end | ||
|
||
it 'fails if fail_if_no_examples set to true' do | ||
write_file 'spec/example_spec.rb', no_examples_custom_failure_exit_code(true) | ||
run_command "" | ||
expect(last_cmd_stdout).to include("0 examples, 0 failures") | ||
expect(last_cmd_exit_status).to eq(15) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters