Sha256: 76394e4ed7551afa4fe7642f53d14a5afccab7db17c3d2ccdefcc99243ae440c

Contents?: true

Size: 1.69 KB

Versions: 27

Compression:

Stored size: 1.69 KB

Contents

module NdrDevSupport
  module IntegrationTesting
    # Grudging handling of flakey integration tests. Allows tests to be declared
    # with `flakey_test`. Our CI reporter gathers information on flakey failures.
    module FlakeyTests
      extend ActiveSupport::Concern

      included do
        class_attribute :attempts_per_test, default: {}
      end

      class_methods do
        def flakey_test(description, attempts: 3, &block)
          test(description, &block).tap do |test_name|
            self.attempts_per_test = attempts_per_test.merge(test_name.to_s => attempts)
          end
        end

        def test_repeatedly(description, times: 100, &block)
          (1..times).map do |n|
            test("#{description} - #{n}/#{times}", &block)
          end
        end
      end

      def flakes
        @flakes ||= []
      end

      def run
        attempts_remaining = attempts_per_test[name]
        return super unless attempts_remaining

        previous_failure = failures.last
        failed_attempts = []
        result = nil

        loop do
          break if attempts_remaining < 1

          result = super

          # No failure was added; we passed!
          break if failures.last == previous_failure

          # Ran out of attempts:
          break if (attempts_remaining -= 1) < 1

          # Loop round and have another go:
          failed_attempts << failures.pop
        end

        # Attempts were only flakey if we eventually passed:
        flakes.concat(failed_attempts) if failures.last == previous_failure

        result
      end
    end
  end
end

if defined?(ActionDispatch)
  ActionDispatch::IntegrationTest.include(NdrDevSupport::IntegrationTesting::FlakeyTests)
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
ndr_dev_support-7.3.1 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.3.0 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.6 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.5 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.4 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.3 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.2 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.1 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.2.0 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.1.0 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-7.0.0 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.9 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.8 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.7 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.6 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.5 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.4 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.3 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.2 lib/ndr_dev_support/integration_testing/flakey_tests.rb
ndr_dev_support-6.1.1 lib/ndr_dev_support/integration_testing/flakey_tests.rb