Module: TestProf::BeforeAll::Minitest

Defined in:
lib/test_prof/recipes/minitest/before_all.rb

Overview

Add before_all hook to Minitest: wrap all examples into a transaction and store instance variables

Defined Under Namespace

Modules: ClassMethods Classes: Executor

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 100

def included(base)
  base.extend ClassMethods

  base.cattr_accessor :parallelized
  if base.respond_to?(:parallelize_teardown)
    base.parallelize_teardown do
      last_klass = ::Minitest.previous_klass
      if last_klass&.respond_to?(:parallelized) && last_klass&.parallelized
        last_klass.before_all_executor&.deactivate!
      end
    end
  end

  if base.respond_to?(:parallelize)
    base.singleton_class.prepend(Module.new do
      def parallelize(workers: :number_of_processors, with: :processes)
        # super.parallelize returns nil when no parallelization is set up
        if super.nil?
          return
        end

        case with
        when :processes
          self.parallelized = true
        when :threads
          warn "!!! before_all is not implemented for parallalization with threads and " \
            "could work incorrectly"
        else
          warn "!!! tests are using an unknown parallelization strategy and before_all " \
            "could work incorrectly"
        end
      end
    end)
  end
end