| 1 |
Feature: `--example` option |
| 2 |
|
| 3 |
Use the `--example` (or `-e`) option to filter examples by name. |
| 4 |
|
| 5 |
The argument is matched against the full description of the example, which is |
| 6 |
the concatenation of descriptions of the group (including any nested groups) |
| 7 |
and the example. |
| 8 |
|
| 9 |
This allows you to run a single uniquely named example, all examples with |
| 10 |
similar names, all the examples in a uniquely named group, etc, etc. |
| 11 |
|
| 12 |
You can also use the option more than once to specify multiple example |
| 13 |
matches. |
| 14 |
|
| 15 |
Note: description-less examples that have generated descriptions (typical when using the [one-liner syntax](../subject/one-liner-syntax)) cannot be directly filtered with this option, because it is necessary to execute the example to generate the description, so RSpec is unable to use the not-yet-generated description to decide whether or not to execute an example. You can, of course, pass part of a group's description to select all examples defined in the group (including those that have no description). |
| 16 |
|
| 17 |
Background: |
| 18 |
Given a file named "first_spec.rb" with: |
| 19 |
"""ruby |
| 20 |
RSpec.describe "first group" do |
| 21 |
it "first example in first group" do; end |
| 22 |
it "second example in first group" do; end |
| 23 |
end |
| 24 |
""" |
| 25 |
And a file named "second_spec.rb" with: |
| 26 |
"""ruby |
| 27 |
RSpec.describe "second group" do |
| 28 |
it "first example in second group" do; end |
| 29 |
it "second example in second group" do; end |
| 30 |
end |
| 31 |
""" |
| 32 |
And a file named "third_spec.rb" with: |
| 33 |
"""ruby |
| 34 |
RSpec.describe "third group" do |
| 35 |
it "first example in third group" do; end |
| 36 |
context "nested group" do |
| 37 |
it "first example in nested group" do; end |
| 38 |
it "second example in nested group" do; end |
| 39 |
end |
| 40 |
end |
| 41 |
""" |
| 42 |
And a file named "fourth_spec.rb" with: |
| 43 |
"""ruby |
| 44 |
RSpec.describe Array do |
| 45 |
describe "#length" do |
| 46 |
it "is the number of items" do |
| 47 |
expect(Array.new([1,2,3]).length).to eq 3 |
| 48 |
end |
| 49 |
end |
| 50 |
end |
| 51 |
""" |
| 52 |
|
| 53 |
Scenario: No matches |
| 54 |
When I run `rspec . --example nothing_like_this` |
| 55 |
Then the process should succeed even though no examples were run |
| 56 |
|
| 57 |
Scenario: Match on one word |
| 58 |
When I run `rspec . --example example` |
| 59 |
Then the examples should all pass |
| 60 |
|
| 61 |
Scenario: One match in each context |
| 62 |
When I run `rspec . --example 'first example'` |
| 63 |
Then the examples should all pass |
| 64 |
|
| 65 |
Scenario: One match in one file using just the example name |
| 66 |
When I run `rspec . --example 'first example in first group'` |
| 67 |
Then the examples should all pass |
| 68 |
|
| 69 |
Scenario: One match in one file using the example name and the group name |
| 70 |
When I run `rspec . --example 'first group first example in first group'` |
| 71 |
Then the examples should all pass |
| 72 |
|
| 73 |
Scenario: All examples in one group |
| 74 |
When I run `rspec . --example 'first group'` |
| 75 |
Then the examples should all pass |
| 76 |
|
| 77 |
Scenario: One match in one file with group name |
| 78 |
When I run `rspec . --example 'second group first example'` |
| 79 |
Then the examples should all pass |
| 80 |
|
| 81 |
Scenario: All examples in one group including examples in nested groups |
| 82 |
When I run `rspec . --example 'third group'` |
| 83 |
Then the examples should all pass |
| 84 |
|
| 85 |
Scenario: Match using `ClassName#method_name` form |
| 86 |
When I run `rspec . --example 'Array#length'` |
| 87 |
Then the examples should all pass |
| 88 |
|
| 89 |
Scenario: Multiple applications of example name option |
| 90 |
When I run `rspec . --example 'first group' --example 'second group' --format d` |
| 91 |
Then the examples should all pass |
| 92 |
And the output should contain all of these: |
| 93 |
|first example in first group| |
| 94 |
|second example in first group| |
| 95 |
|first example in second group| |
| 96 |
|second example in second group| |
| 97 |
And the output should not contain any of these: |
| 98 |
|first example in third group| |
| 99 |
|nested group first example in nested group| |
| 100 |
|nested group second example in nested group| |