Skip to content

Commit 324720a

Browse files
author
Federico Aldunate
committed
Provide the possibility of a callback to do something when a flaky spec is deteced
1 parent b16f476 commit 324720a

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ RSpec.configure do |config|
5151
ex.run_with_retry retry: 3
5252
end
5353

54+
# callback to be run when a flaky test is detected
55+
config.flaky_test_callback = proc do |example|
56+
Rspec::Watchdog::Reporter.report(example)
57+
end
58+
5459
# callback to be run between retries
5560
config.retry_callback = proc do |ex|
5661
# run some additional clean up task - can be filtered by example metadata
@@ -90,7 +95,7 @@ You can call `ex.run_with_retry(opts)` on an individual example.
9095
- __:exceptions_to_hard_fail__(default: *[]*) List of exceptions that will trigger an immediate test failure without retry. Takes precedence over __:exceptions_to_retry__
9196
- __:exceptions_to_retry__(default: *[]*) List of exceptions that will trigger a retry (when empty, all exceptions will)
9297
- __:retry_callback__(default: *nil*) Callback function to be called between retries
93-
98+
- __:flaky_test_callback__(default: *nil*) Callback function to be called when a flaky test is detected (when a test fails but then passes on a subsequent attempt)p
9499

95100
## Environment Variables
96101
- __RSPEC_RETRY_RETRY_COUNT__ can override the retry counts even if a retry count is set in an example or default_retry_count is set in a configuration.

lib/rspec/rebound.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def self.setup
3333
# Callback between retries
3434
config.add_setting :retry_callback, :default => nil
3535

36+
# Callback for flaky tests
37+
config.add_setting :flaky_test_callback, :default => nil
38+
3639
config.around(:each) do |ex|
3740
ex.run_with_retry
3841
end
@@ -122,6 +125,14 @@ def run
122125
example.clear_exception
123126
ex.run
124127

128+
if example.exception.nil?
129+
# If it's a flaky test, call the callback
130+
if attempts > 0 && RSpec.configuration.flaky_test_callback
131+
132+
example.example_group_instance.instance_exec(example, &RSpec.configuration.flaky_test_callback)
133+
end
134+
end
135+
125136
self.attempts += 1
126137

127138
break if example.exception.nil?

rspec-rebound.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
1919
gem.version = RSpec::Rebound::VERSION
2020
gem.add_runtime_dependency "rspec-core", "~> 3.3"
2121
gem.add_development_dependency "rspec", "~> 3.3"
22+
gem.add_development_dependency "debug", "~> 1.0"
2223
end

spec/spec_helper.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
require 'rspec/core/sandbox'
33

44
require 'rspec/rebound'
5-
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2')
6-
require "pry-debugger"
7-
else
8-
require "pry-byebug"
5+
if Gem::Version.new(RUBY_VERSION) > Gem::Version.new('3')
6+
require 'debug'
97
end
108

119
RSpec.configure do |config|

0 commit comments

Comments
 (0)