Skip to content

Commit

Permalink
Merge pull request #80 from abrom/master
Browse files Browse the repository at this point in the history
* abrom-master:
  add metadata to test case
  explicitly fail / retry
  rename control to retry_callback_called
  Add `retry_callback` to allow for custom cleanup between retries
  • Loading branch information
michaelglass committed Aug 22, 2017
2 parents e0dfd27 + 637356e commit d6f4ef1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ RSpec.configure do |config|
config.around :each, :js do |ex|
ex.run_with_retry retry: 3
end

# callback to be run between retries
config.retry_callback = proc do |ex|
# run some additional clean up task - can be filtered by example metadata
if ex.metadata[:js]
Capybara.reset!
end
end
end
```

Expand Down Expand Up @@ -73,6 +81,8 @@ You can call `ex.run_with_retry(opts)` on an individual example.
- __:clear_lets_on_failure__(default: *true*) Clear memoized values for ``let``s before retrying
- __:exceptions_to_hard_fail__(default: *[]*) List of exceptions that will trigger an immediate test failure without retry. Takes precedence over __:exceptions_to_retry__
- __:exceptions_to_retry__(default: *[]*) List of exceptions that will trigger a retry (when empty, all exceptions will)
- __:retry_callback__(default: *nil*) Callback function to be called between retries


## Environment Variables
- __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.
Expand Down
8 changes: 8 additions & 0 deletions lib/rspec/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def self.setup
# If no list of exceptions is provided and 'retry' > 1, we always retry.
config.add_setting :exceptions_to_retry, :default => []

# Callback between retries
config.add_setting :retry_callback, :default => nil

config.around(:each) do |ex|
ex.run_with_retry
end
Expand Down Expand Up @@ -145,6 +148,11 @@ def run

example.example_group_instance.clear_lets if clear_lets

# If the callback is defined, let's call it
if RSpec.configuration.retry_callback
example.example_group_instance.instance_exec(example, &RSpec.configuration.retry_callback)
end

sleep sleep_interval if sleep_interval.to_i > 0
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/lib/rspec/retry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,54 @@ class SharedError < StandardError; end;
end
end

describe 'calling retry_callback between retries', retry: 2 do
before(:all) do
RSpec.configuration.retry_callback = proc do |example|
@retry_callback_called = true
@example = example
end
end

after(:all) do
RSpec.configuration.retry_callback = nil
end

context 'if failure' do
before(:all) do
@retry_callback_called = false
@example = nil
@retry_attempts = 0
end

it 'should call retry callback', with_some: 'metadata' do |example|
if @retry_attempts == 0
@retry_attempts += 1
expect(@retry_callback_called).to be(false)
expect(@example).to eq(nil)
raise "let's retry once!"
elsif @retry_attempts > 0
expect(@retry_callback_called).to be(true)
expect(@example).to eq(example)
expect(@example.metadata[:with_some]).to eq('metadata')
end
end
end

context 'does not call retry_callback if no errors' do
before(:all) do
@retry_callback_called = false
@example = nil
end

after do
expect(@retry_callback_called).to be(false)
expect(@example).to be_nil
end

it { true }
end
end

describe 'output in verbose mode' do

line_1 = __LINE__ + 8
Expand Down

0 comments on commit d6f4ef1

Please sign in to comment.