mardi 26 juillet 2016

RSpec with Capybara, rerun an entire feature if a scenario fails

I’m currently using RSpec and Capybara to test a website. Say I have a feature file such as this.

Feature: A simple feature
  Scenario: name monster
    Given there is a monster called "John Smith"
    Then it should be called "John Smith"

  Scenario: change monster name
    Given there is a monster called "John Smith"
    When I change its name to ""
    Then it should be nameless

  Scenario: name monster john
    Given there is a monster called John
    Then it should be called "John"

I would like to run this feature file so that if an individual scenario fails then the entire feature file will be rerun and if it fails again the test will fail as normal. Otherwise, if on the second run through it passes it should continue as normal.

So in accordance with the above feature, if the scenario "name monster john" fails then the file should be run from the beginning, starting with the first scenario, "name monster".

I have tried using the code below, which will rerun only a scenario (not the all the scenarios) if it fails up to three times at which point it will permanently fail. What I am trying to do however is rerun the entire feature file from the beginning.

require 'rspec/core'
require 'rspec/flaky/version'
require 'rspec_ext/rspec_ext'

 module RSpec
  class Flaky
    def self.apply
      RSpec.configure do |conf|
        conf.add_setting :verbose_retry_flaky_example, default: false
        conf.add_setting :flaky_retry_count, default: 1
        conf.add_setting :flaky_sleep_interval, default: 0

        # from rspec/rspec-core
        # context.example is deprecated, but RSpec.current_example is not
        # available until RSpec 3.0.
        fetch_current_example = RSpec.respond_to?(:current_example) ?
            proc { RSpec.current_example } : proc { |context| context.example    }

        conf.around :all, type: :feature do |example|
          retry_count = RSpec.configuration.flaky_retry_count
          sleep_interval = RSpec.configuration.flaky_sleep_interval

          # from rspec/rspec-core
          current_example = fetch_current_example.call(self)

          retry_count.times do |r_count|
            if RSpec.configuration.verbose_retry_flaky_example && r_count > 0
              msg = "\n Test failed! Retrying...This is retry: #{r_count}
                    \n Failed at:  #{example.location}"
              RSpec.configuration.reporter.message msg
            end

            current_example.clear_exception
            example.run

            break if current_example.exception.nil?

            sleep sleep_interval if sleep_interval > 0
          end

        end # conf.around
      end # RSpec.configure
    end # def self.apply
  end # class Flaky
end # module RSpec

RSpec::Flaky.apply

Does RSpec have any way to rerun an entire feature or is this not possible?

Aucun commentaire:

Enregistrer un commentaire