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/setup/webmock.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Assertions

#assert, #diff, #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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/minitest/utils/extension.rb', line 174

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

.method_added(method_name) ⇒ Object

This hook handles methods defined directly with def test_.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/minitest/utils/extension.rb', line 77

def self.method_added(method_name)
  super

  test_name = method_name.to_s

  return unless test_name.start_with?("test_")

  klass = name
  id = "#{klass}##{method_name}"
  description = method_name.to_s.delete_prefix("test_").tr("_", " ")

  return if Test.tests[id]

  file_path, lineno = instance_method(method_name).source_location

  source_location = [
    Pathname(file_path).relative_path_from(Pathname(Dir.pwd)),
    lineno
  ]

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

.setup(&block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/minitest/utils/extension.rb', line 150

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



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/minitest/utils/extension.rb', line 162

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



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/minitest/utils/extension.rb', line 107

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



67
68
69
70
71
72
73
74
# File 'lib/minitest/utils/extension.rb', line 67

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

.testsObject



57
58
59
# File 'lib/minitest/utils/extension.rb', line 57

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

Instance Method Details

#slow_testObject



61
62
63
64
65
# File 'lib/minitest/utils/extension.rb', line 61

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

  skip "slow test"
end