| 1 |
Feature: Using the `--only-failures` option |
| 2 |
|
| 3 |
The `--only-failures` option filters what examples are run so that only those that failed the last time they ran are executed. To use this option, you first have to configure `config.example_status_persistence_file_path`, which RSpec will use to store the status of each example the last time it ran. |
| 4 |
|
| 5 |
There's also a `--next-failure` option, which is shorthand for `--only-failures --fail-fast --order defined`. It allows you to repeatedly focus on just one of the currently failing examples, then move on to the next failure, etc. |
| 6 |
|
| 7 |
Either of these options can be combined with another a directory or file name; RSpec will run just the failures from the set of loaded examples. |
| 8 |
|
| 9 |
Background: |
| 10 |
Given a file named "spec/spec_helper.rb" with: |
| 11 |
"""ruby |
| 12 |
RSpec.configure do |c| |
| 13 |
c.example_status_persistence_file_path = "examples.txt" |
| 14 |
end |
| 15 |
""" |
| 16 |
And a file named ".rspec" with: |
| 17 |
""" |
| 18 |
--require spec_helper |
| 19 |
--order random |
| 20 |
--format documentation |
| 21 |
""" |
| 22 |
And a file named "spec/array_spec.rb" with: |
| 23 |
"""ruby |
| 24 |
RSpec.describe 'Array' do |
| 25 |
it "checks for inclusion of 1" do |
| 26 |
expect([1, 2]).to include(1) |
| 27 |
end |
| 28 |
|
| 29 |
it "checks for inclusion of 2" do |
| 30 |
expect([1, 2]).to include(2) |
| 31 |
end |
| 32 |
|
| 33 |
it "checks for inclusion of 3" do |
| 34 |
expect([1, 2]).to include(3) # failure |
| 35 |
end |
| 36 |
end |
| 37 |
""" |
| 38 |
And a file named "spec/string_spec.rb" with: |
| 39 |
"""ruby |
| 40 |
RSpec.describe 'String' do |
| 41 |
it "checks for inclusion of 'foo'" do |
| 42 |
expect("food").to include('foo') |
| 43 |
end |
| 44 |
|
| 45 |
it "checks for inclusion of 'bar'" do |
| 46 |
expect("food").to include('bar') # failure |
| 47 |
end |
| 48 |
|
| 49 |
it "checks for inclusion of 'baz'" do |
| 50 |
expect("bazzy").to include('baz') |
| 51 |
end |
| 52 |
|
| 53 |
it "checks for inclusion of 'foobar'" do |
| 54 |
expect("food").to include('foobar') # failure |
| 55 |
end |
| 56 |
end |
| 57 |
""" |
| 58 |
And a file named "spec/passing_spec.rb" with: |
| 59 |
"""ruby |
| 60 |
puts "Loading passing_spec.rb" |
| 61 |
|
| 62 |
RSpec.describe "A passing spec" do |
| 63 |
it "passes" do |
| 64 |
expect(1).to eq(1) |
| 65 |
end |
| 66 |
end |
| 67 |
""" |
| 68 |
And I have run `rspec` once, resulting in "8 examples, 3 failures" |
| 69 |
|
| 70 |
Scenario: Running `rspec --only-failures` loads only spec files with failures and runs only the failures |
| 71 |
When I run `rspec --only-failures` |
| 72 |
Then the output from "rspec --only-failures" should contain "3 examples, 3 failures" |
| 73 |
And the output from "rspec --only-failures" should not contain "Loading passing_spec.rb" |
| 74 |
|
| 75 |
Scenario: Combine `--only-failures` with a file name |
| 76 |
When I run `rspec spec/array_spec.rb --only-failures` |
| 77 |
Then the output should contain "1 example, 1 failure" |
| 78 |
When I run `rspec spec/string_spec.rb --only-failures` |
| 79 |
Then the output should contain "2 examples, 2 failures" |
| 80 |
|
| 81 |
Scenario: Use `--next-failure` to repeatedly run a single failure |
| 82 |
When I run `rspec --next-failure` |
| 83 |
Then the output should contain "1 example, 1 failure" |
| 84 |
And the output should contain "checks for inclusion of 3" |
| 85 |
|
| 86 |
When I fix "spec/array_spec.rb" by replacing "to include(3)" with "not_to include(3)" |
| 87 |
And I run `rspec --next-failure` |
| 88 |
Then the output should contain "2 examples, 1 failure" |
| 89 |
And the output should contain "checks for inclusion of 3" |
| 90 |
And the output should contain "checks for inclusion of 'bar'" |
| 91 |
|
| 92 |
When I fix "spec/string_spec.rb" by replacing "to include('bar')" with "not_to include('bar')" |
| 93 |
And I run `rspec --next-failure` |
| 94 |
Then the output should contain "2 examples, 1 failure" |
| 95 |
And the output should contain "checks for inclusion of 'bar'" |
| 96 |
And the output should contain "checks for inclusion of 'foobar'" |
| 97 |
|
| 98 |
When I fix "spec/string_spec.rb" by replacing "to include('foobar')" with "not_to include('foobar')" |
| 99 |
And I run `rspec --next-failure` |
| 100 |
Then the output should contain "1 example, 0 failures" |
| 101 |
And the output should contain "checks for inclusion of 'foobar'" |
| 102 |
|
| 103 |
When I run `rspec --next-failure` |
| 104 |
Then the output should contain "All examples were filtered out" |
| 105 |
|
| 106 |
Scenario: Running `rspec --only-failures` with spec files that pass doesn't run anything |
| 107 |
When I run `rspec spec/passing_spec.rb --only-failures` |
| 108 |
Then it should pass with "0 examples, 0 failures" |
| 109 |
|
| 110 |
Scenario: Clear error given when using `--only-failures` without configuring `example_status_persistence_file_path` |
| 111 |
Given I have not configured `example_status_persistence_file_path` |
| 112 |
When I run `rspec --only-failures` |
| 113 |
Then it should fail with "To use `--only-failures`, you must first set `config.example_status_persistence_file_path`." |