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

Commit 9e801c3

Browse files
committed
Merge pull request #2013 from rspec/add_example_finished_notification
Add example_finished to the formatter protocol
2 parents 0697d3a + 2e14feb commit 9e801c3

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Enhancements:
99
objects, replacing Ruby's excessively verbose output. (Gavin Miller, #1922)
1010
* Add `silence_filter_announcements` configuration option.
1111
(David Raffensperger, #2007)
12+
* Add optional `example_finished` notification to the reporter protocol for
13+
when you don't care about the example outcome. (Jon Rowe, #2013)
1214

1315
### 3.3.1 / 2015-06-18
1416
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.3.0...v3.3.1)

lib/rspec/core/example.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ def start(reporter)
406406
def finish(reporter)
407407
pending_message = execution_result.pending_message
408408

409+
reporter.example_finished(self)
409410
if @exception
410411
record_finished :failed
411412
execution_result.exception = @exception

lib/rspec/core/formatters/protocol.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ class Protocol
6666
# @param notification [ExampleNotification] containing example subclass
6767
# of `RSpec::Core::Example`
6868

69+
# @method example_finished
70+
# @api public
71+
# @group Example Notifications
72+
#
73+
# Invoked at the end of the execution of each example.
74+
#
75+
# @param notification [ExampleNotification] containing example subclass
76+
# of `RSpec::Core::Example`
77+
6978
# @method example_passed
7079
# @api public
7180
# @group Example Notifications

lib/rspec/core/reporter.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Reporter
88
:close, :deprecation, :deprecation_summary, :dump_failures, :dump_pending,
99
:dump_profile, :dump_summary, :example_failed, :example_group_finished,
1010
:example_group_started, :example_passed, :example_pending, :example_started,
11-
:message, :seed, :start, :start_dump, :stop
11+
:message, :seed, :start, :start_dump, :stop, :example_finished
1212
])
1313

1414
def initialize(configuration)
@@ -124,6 +124,11 @@ def example_started(example)
124124
notify :example_started, Notifications::ExampleNotification.for(example)
125125
end
126126

127+
# @private
128+
def example_finished(example)
129+
notify :example_finished, Notifications::ExampleNotification.for(example)
130+
end
131+
127132
# @private
128133
def example_passed(example)
129134
notify :example_passed, Notifications::ExampleNotification.for(example)

spec/rspec/core/reporter_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,47 @@ module RSpec::Core
7676
reporter.example_started(example)
7777
end
7878

79-
it "passes example_group_started and example_group_finished messages to that formatter in that order" do
79+
it "passes messages to the formatter in the correct order" do
8080
order = []
8181

8282
formatter = double("formatter")
8383
allow(formatter).to receive(:example_group_started) { |n| order << "Started: #{n.group.description}" }
84+
allow(formatter).to receive(:example_started) { |n| order << "Started Example" }
85+
allow(formatter).to receive(:example_finished) { |n| order << "Finished Example" }
86+
allow(formatter).to receive(:example_passed) { |n| order << "Passed" }
87+
allow(formatter).to receive(:example_pending) { |n| order << "Pending" }
88+
allow(formatter).to receive(:example_failed) { |n| order << "Failed" }
8489
allow(formatter).to receive(:example_group_finished) { |n| order << "Finished: #{n.group.description}" }
8590

86-
reporter.register_listener formatter, :example_group_started, :example_group_finished
91+
reporter.register_listener formatter, :example_group_started, :example_group_finished,
92+
:example_started, :example_finished,
93+
:example_passed, :example_failed, :example_pending
8794

8895
group = RSpec.describe("root")
8996
group.describe("context 1") do
90-
example("ignore") {}
97+
example("passing example") {}
98+
example("pending example", :skip => true) { }
9199
end
92100
group.describe("context 2") do
93-
example("ignore") {}
101+
example("failed example") { fail }
94102
end
95103

96104
group.run(reporter)
97105

98106
expect(order).to eq([
99107
"Started: root",
100108
"Started: context 1",
109+
"Started Example",
110+
"Finished Example",
111+
"Passed",
112+
"Started Example",
113+
"Finished Example",
114+
"Pending",
101115
"Finished: context 1",
102116
"Started: context 2",
117+
"Started Example",
118+
"Finished Example",
119+
"Failed",
103120
"Finished: context 2",
104121
"Finished: root"
105122
])

0 commit comments

Comments
 (0)