Class: Minitest::Test

Inherits:
Object
  • Object
show all
Includes:
FactoryBot::Syntax::Methods, FactoryGirl::Syntax::Methods, Utils::Assertions
Defined in:
lib/minitest/utils/reporter.rb,
lib/minitest/utils/extension.rb,
lib/minitest/utils/setup/factory_bot.rb,
lib/minitest/utils/setup/factory_girl.rb,
lib/minitest/utils/setup/database_cleaner.rb,
lib/minitest/utils/capybara/screenshot_on_failures.rb,
lib/minitest/utils/setup/webmock.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Assertions

#assert, #refute

Class Attribute Details

.slow_thresholdObject

Returns the value of attribute slow_threshold.



8
9
10
# File 'lib/minitest/utils/reporter.rb', line 8

def slow_threshold
  @slow_threshold
end

Class Method Details

.inherited(child) ⇒ Object



11
12
13
14
# File 'lib/minitest/utils/reporter.rb', line 11

def self.inherited(child)
  child.slow_threshold = slow_threshold
  super
end

.let(name, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/minitest/utils/extension.rb', line 107

def self.let(name, &block)
  target = begin
    instance_method(name)
  rescue StandardError
    nil
  end

  message = "Cannot define let(:#{name});"

  if name.to_s.start_with?("test")
    raise ArgumentError, "#{message} method cannot begin with 'test'."
  end

  if target
    raise ArgumentError,
          "#{message} method already defined by #{target.owner}."
  end

  define_method(name) do
    @_memoized ||= {}
    @_memoized.fetch(name) {|k| @_memoized[k] = instance_eval(&block) }
  end
end

.setup(&block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/minitest/utils/extension.rb', line 83

def self.setup(&block)
  mod = Module.new
  mod.module_eval do
    define_method :setup do
      super()
      instance_eval(&block)
    end
  end

  include mod
end

.teardown(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/minitest/utils/extension.rb', line 95

def self.teardown(&block)
  mod = Module.new
  mod.module_eval do
    define_method :teardown do
      super()
      instance_eval(&block)
    end
  end

  include mod
end

.test(description, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/minitest/utils/extension.rb', line 40

def self.test(description, &block)
  source_location = caller_locations(1..1).first
  source_location = [
    Pathname(source_location.path).relative_path_from(Pathname(Dir.pwd)),
    source_location.lineno
  ]

  klass = name
  test_name = test_method_name(description)
  defined = method_defined?(test_name)
  id = "#{klass}##{test_name}"

  Test.tests[id] = {
    id:,
    description:,
    name: test_name,
    source_location:,
    time: nil,
    slow_threshold:
  }

  testable = proc do
    err = nil
    t0 = Minitest.clock_time
    instance_eval(&block)
  rescue StandardError => error
    err = error
  ensure
    Test.tests["#{klass}##{test_name}"][:time] = Minitest.clock_time - t0
    raise err if err
  end

  raise "#{test_name} is already defined in #{self}" if defined

  if block
    define_method(test_name, &testable)
  else
    define_method(test_name) do
      flunk "No implementation provided for #{name}"
    end
  end
end

.test_method_name(description) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/minitest/utils/extension.rb', line 31

def self.test_method_name(description)
  method_name = description.downcase
                           .gsub(/[^a-z0-9]+/, "_")
                           .gsub(/^_+/, "")
                           .gsub(/_+$/, "")
                           .squeeze("_")
  :"test_#{method_name}"
end

.testsObject



21
22
23
# File 'lib/minitest/utils/extension.rb', line 21

def self.tests
  @tests ||= {}
end

Instance Method Details

#slow_testObject



25
26
27
28
29
# File 'lib/minitest/utils/extension.rb', line 25

def slow_test
  return if ENV["MT_RUN_SLOW_TESTS"] || Minitest.options[:slow]

  skip "slow test"
end