如果场景失败,RSpec与Capybara重新运行整个功能

时间:2016-07-27 04:58:09

标签: ruby testing rspec capybara

我目前正在使用RSpec和Capybara来测试网站。假设我有一个这样的功能文件。

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"

我想运行此功能文件,以便在单个方案失败时,将重新运行整个功能文件,如果再次失败,则测试将正常失败。否则,如果第二次运行它通过它应该继续正常。

所以按照上面的特点,如果场景"名称怪物john"失败然后文件应该从头开始运行,从第一个场景开始,"命名怪物"。

我尝试使用下面的代码,如果它最多三次失败,它将只会重新运行一个场景(不是所有场景),此时它将永久失败。然而,我想要做的是从头开始重新运行整个功能文件。

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

RSpec有没有办法重新运行整个功能,或者这是不可能的?

0 个答案:

没有答案