Class: Minitest::Test

Inherits:
Runnable show all
Extended by:
Guard
Includes:
Assertions, Guard, Reportable, LifecycleHooks
Defined in:
lib/minitest/test.rb,
lib/minitest/hell.rb

Overview

Subclass Test to create your own tests. Typically you’ll want a Test subclass per implementation class.

See Minitest::Assertions

Direct Known Subclasses

Benchmark, Spec

Defined Under Namespace

Modules: LifecycleHooks

Constant Summary collapse

PASSTHROUGH_EXCEPTIONS =

:nodoc:

[NoMemoryError, SignalException, SystemExit]
SETUP_METHODS =

:nodoc:

%w[ before_setup setup after_setup ]
TEARDOWN_METHODS =

:nodoc:

%w[ before_teardown teardown after_teardown ]

Constants included from Assertions

Assertions::UNDEFINED

Constants included from Reportable

Reportable::BASE_DIR

Constants inherited from Runnable

Runnable::SIGNALS

Class Attribute Summary collapse

Attributes inherited from Runnable

#assertions, #failures, #metadata, #time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Guard

jruby?, mri?, osx?, windows?

Methods included from LifecycleHooks

#after_setup, #after_teardown, #before_setup, #before_teardown, #setup, #teardown

Methods included from Assertions

#_synchronize, #_where, #assert, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_output, #assert_path_exists, #assert_pattern, #assert_predicate, #assert_raises, #assert_respond_to, #assert_same, #assert_silent, #assert_throws, #capture_io, #capture_subprocess_io, #diff, diff, diff=, #exception_details, #fail_after, #flunk, #message, #mu_pp, #mu_pp_for_diff, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_path_exists, #refute_pattern, #refute_predicate, #refute_respond_to, #refute_same, #skip, #skip_until, #skipped?, #things_to_diff

Methods included from Reportable

#class_name, #error?, #location, #passed?, #result_code, #skipped?

Methods inherited from Runnable

#failure, filter_runnable_methods, inherited, #initialize, #metadata?, methods_matching, #name, #name=, on_signal, #passed?, reset, #result_code, run, run_order, run_suite, runnables, #skipped?, #time_it, with_info_handler

Constructor Details

This class inherits a constructor from Minitest::Runnable

Class Attribute Details

.io_lockObject

Returns the value of attribute io_lock.



22
23
24
# File 'lib/minitest/test.rb', line 22

def io_lock
  @io_lock
end

Class Method Details

.i_suck_and_my_tests_are_order_dependent!Object

Call this at the top of your tests when you absolutely positively need to have ordered tests. In doing so, you’re admitting that you suck and your tests are weak.



31
32
33
34
35
36
# File 'lib/minitest/test.rb', line 31

def self.i_suck_and_my_tests_are_order_dependent!
  class << self
    undef_method :run_order if method_defined? :run_order
    define_method :run_order do :alpha end
  end
end

.make_my_diffs_pretty!Object

Make diffs for this Test use #pretty_inspect so that diff in assert_equal can have more details. NOTE: this is much slower than the regular inspect but much more usable for complex objects.



44
45
46
47
48
# File 'lib/minitest/test.rb', line 44

def self.make_my_diffs_pretty!
  require "pp"

  define_method :mu_pp, &:pretty_inspect
end

.parallelize_me!Object

Call this at the top of your tests (inside the Minitest::Test subclass or describe block) when you want to run your tests in parallel. In doing so, you’re admitting that you rule and your tests are awesome.



56
57
58
59
60
# File 'lib/minitest/test.rb', line 56

def self.parallelize_me!
  return unless Minitest.parallel_executor
  include Minitest::Parallel::Test
  extend Minitest::Parallel::Test::ClassMethods
end

.runnable_methodsObject

Returns all instance methods starting with “test_”. Based on #run_order, the methods are either sorted, randomized (default), or run in parallel.



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/minitest/test.rb', line 67

def self.runnable_methods
  methods = methods_matching(/^test_/)

  case self.run_order
  when :random, :parallel then
    srand Minitest.seed
    methods.sort.shuffle
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown_order: %p" % [self.run_order]
  end
end

Instance Method Details

#capture_exceptionsObject

LifecycleHooks



186
187
188
189
190
191
192
193
194
# File 'lib/minitest/test.rb', line 186

def capture_exceptions # :nodoc:
  yield
rescue *PASSTHROUGH_EXCEPTIONS
  raise
rescue Assertion => e
  self.failures << e
rescue Exception => e
  self.failures << UnexpectedError.new(sanitize_exception e)
end

#neuter_exception(e) ⇒ Object

:nodoc:



203
204
205
206
207
208
209
210
211
212
# File 'lib/minitest/test.rb', line 203

def neuter_exception e # :nodoc:
  bt = e.backtrace
  msg = e.message.dup

  new_exception e.class, msg, bt            # e.class can be a problem...
rescue
  msg.prepend "Neutered Exception #{e.class}: "

  new_exception RuntimeError, msg, bt, true # but if this raises, we die
end

#new_exception(klass, msg, bt, kill = false) ⇒ Object

:nodoc:



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/minitest/test.rb', line 214

def new_exception klass, msg, bt, kill = false # :nodoc:
  ne = klass.new msg
  ne.set_backtrace bt

  if kill then
    ne.instance_variables.each do |v|
      ne.remove_instance_variable v
    end
  end

  Marshal.dump ne                           # can raise TypeError
  ne
end

#runObject

Runs a single test with setup/teardown hooks.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/minitest/test.rb', line 84

def run
  time_it do
    capture_exceptions do
      SETUP_METHODS.each do |hook|
        self.send hook
      end

      self.send self.name
    end

    TEARDOWN_METHODS.each do |hook|
      capture_exceptions do
        self.send hook
      end
    end
  end

  Result.from self # per contract
end

#sanitize_exception(e) ⇒ Object

:nodoc:



196
197
198
199
200
201
# File 'lib/minitest/test.rb', line 196

def sanitize_exception e # :nodoc:
  Marshal.dump e
  e                                         # good: use as-is
rescue
  neuter_exception e
end