Class: Minitest::Test

Inherits:
Runnable show all
Extended by:
Guard
Includes:
Assertions, Guard, LifecycleHooks
Defined in:
opal/stdlib/minitest/test.rb,
opal/stdlib/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, Unit::TestCase, Test::Unit::TestCase

Defined Under Namespace

Modules: LifecycleHooks

Constant Summary

PASSTHROUGH_EXCEPTIONS =

:nodoc:

[NoMemoryError, SignalException, # :nodoc:
Interrupt, SystemExit]
TEARDOWN_METHODS =

:nodoc:

%w{ before_teardown teardown after_teardown }

Constants included from Assertions

Assertions::E, Assertions::UNDEFINED

Constants inherited from Runnable

Runnable::SIGNALS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Runnable

#assertions, #failures

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Guard

jruby?, maglev?, mri?, rubinius?, windows?

Methods included from LifecycleHooks

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

Methods included from Assertions

#_synchronize, #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_predicate, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #capture_subprocess_io, #diff, diff, diff=, #exception_details, #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_predicate, #refute_respond_to, #refute_same, #skip

Methods inherited from Runnable

#failure, inherited, #initialize, methods_matching, #name, #name=, on_signal, reset, run, run_one_method, runnables, 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



17
18
19
# File 'opal/stdlib/minitest/test.rb', line 17

def io_lock
  @io_lock
end

Instance Attribute Details

#timeObject

The time it took to run this test.



86
87
88
# File 'opal/stdlib/minitest/test.rb', line 86

def time
  @time
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.



25
26
27
28
29
30
# File 'opal/stdlib/minitest/test.rb', line 25

def self.i_suck_and_my_tests_are_order_dependent!
  class << self
    undef_method :test_order if method_defined? :test_order
    define_method :test_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.



38
39
40
41
42
43
44
# File 'opal/stdlib/minitest/test.rb', line 38

def self.make_my_diffs_pretty!
  require "pp"

  define_method :mu_pp do |o|
    o.pretty_inspect
  end
end

.old_test_orderObject

:nodoc:



5
# File 'opal/stdlib/minitest/hell.rb', line 5

alias :old_test_order :test_order

.parallelize_me!Object

Call this at the top of your tests when you want to run your tests in parallel. In doing so, you're admitting that you rule and your tests are awesome.



51
52
53
54
# File 'opal/stdlib/minitest/test.rb', line 51

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

.runnable_methodsObject

Returns all instance methods starting with "test_". Based on

test_order, the methods are either sorted, randomized

(default), or run in parallel.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'opal/stdlib/minitest/test.rb', line 61

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

  case self.test_order
  when :random, :parallel then
    max = methods.size
    methods.sort.sort_by { rand max }
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown test_order: #{self.test_order.inspect}"
  end
end

.test_orderObject

Defines the order to run tests (:random by default). Override this or use a convenience method to change it for your tests.



7
8
9
# File 'opal/stdlib/minitest/hell.rb', line 7

def test_order # :nodoc:
  :parallel
end

Instance Method Details

#capture_exceptionsObject

LifecycleHooks



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'opal/stdlib/minitest/test.rb', line 204

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

#error?Boolean

Did this run error?

Returns:



225
226
227
# File 'opal/stdlib/minitest/test.rb', line 225

def error?
  self.failures.any? { |f| UnexpectedError === f }
end

#locationObject

The location identifier of this test.



232
233
234
235
# File 'opal/stdlib/minitest/test.rb', line 232

def location
  loc = " [#{self.failure.location}]" unless passed? or error?
  "#{self.class}##{self.name}#{loc}"
end

#marshal_dumpObject

:nodoc:



88
89
90
# File 'opal/stdlib/minitest/test.rb', line 88

def marshal_dump # :nodoc:
  super << self.time
end

#marshal_load(ary) ⇒ Object

:nodoc:



92
93
94
95
# File 'opal/stdlib/minitest/test.rb', line 92

def marshal_load ary # :nodoc:
  self.time = ary.pop
  super
end

#passed?Boolean

Did this run pass?

Note: skipped runs are not considered passing, but they don't cause the process to exit non-zero.

Returns:



243
244
245
# File 'opal/stdlib/minitest/test.rb', line 243

def passed?
  not self.failure
end

#result_codeObject

Returns ".", "F", or "E" based on the result of the run.



250
251
252
# File 'opal/stdlib/minitest/test.rb', line 250

def result_code
  self.failure and self.failure.result_code or "."
end

#runObject

Runs a single test with setup/teardown hooks.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'opal/stdlib/minitest/test.rb', line 102

def run
  with_info_handler do
    time_it do
      capture_exceptions do
        before_setup; setup; after_setup

        self.send self.name
      end

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

  self # per contract
end

#skipped?Boolean

Was this run skipped?

Returns:



257
258
259
# File 'opal/stdlib/minitest/test.rb', line 257

def skipped?
  self.failure and Skip === self.failure
end

#time_itObject

:nodoc:



261
262
263
264
265
266
267
# File 'opal/stdlib/minitest/test.rb', line 261

def time_it # :nodoc:
  t0 = Time.now

  yield
ensure
  self.time = Time.now - t0
end

#to_sObject

:nodoc:



269
270
271
272
273
274
275
# File 'opal/stdlib/minitest/test.rb', line 269

def to_s # :nodoc:
  return location if passed? and not skipped?

  failures.map { |failure|
    "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
  }.join "\n"
end

#with_info_handler(&block) ⇒ Object

:nodoc:



277
278
279
280
281
282
283
284
285
# File 'opal/stdlib/minitest/test.rb', line 277

def with_info_handler &block # :nodoc:
  t0 = Time.now

  handler = lambda do
    warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Time.now - t0]
  end

  self.class.on_signal "INFO", handler, &block
end