Module: TestProf::MinitestSample

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

Overview

Add ability to run only a specified number of example groups (randomly selected)

Constant Summary collapse

CORE_RUNNABLES =

Do not add these classes to resulted sample

[
  Minitest::Test,
  defined?(Minitest::Unit::TestCase) ? Minitest::Unit::TestCase : nil,
  defined?(Minitest::Spec) ? Minitest::Spec : nil
].compact.freeze

Class Method Summary collapse

Class Method Details

.callObject



48
49
50
51
52
53
54
# File 'lib/test_prof/recipes/minitest/sample.rb', line 48

def call
  if ENV["SAMPLE"]
    ::TestProf::MinitestSample.sample_examples(ENV["SAMPLE"].to_i)
  elsif ENV["SAMPLE_GROUPS"]
    ::TestProf::MinitestSample.sample_groups(ENV["SAMPLE_GROUPS"].to_i)
  end
end

.sample_examples(sample_size) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/test_prof/recipes/minitest/sample.rb', line 28

def sample_examples(sample_size)
  all_examples = suites.flat_map do |runnable|
    runnable.runnable_methods.map { |method| [runnable, method] }
  end

  sample = all_examples.sample(sample_size).group_by(&:first)
  sample.transform_values! { |v| v.map(&:last) }

  # Filter examples by overriding #runnable_methods for all suites
  suites.each do |runnable|
    if sample.key?(runnable)
      runnable.define_singleton_method(:runnable_methods) do
        super() & sample[runnable]
      end
    else
      runnable.define_singleton_method(:runnable_methods) { [] }
    end
  end
end

.sample_groups(sample_size) ⇒ Object



22
23
24
25
26
# File 'lib/test_prof/recipes/minitest/sample.rb', line 22

def sample_groups(sample_size)
  saved_suites = suites
  Minitest::Runnable.reset
  saved_suites.sample(sample_size).each { |r| Minitest::Runnable.runnables << r }
end

.suitesObject



14
15
16
17
18
19
20
# File 'lib/test_prof/recipes/minitest/sample.rb', line 14

def suites
  # Make sure that sample contains only _real_ suites
  Minitest::Runnable.runnables
    .select do |suite|
      CORE_RUNNABLES.any? { |kl| suite < kl } && suite.runnable_methods.any?
    end
end