123456789_123456789_123456789_123456789_123456789_

Class: Minitest::Test

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Guard, Runnable
Instance Chain:
Inherits: Minitest::Runnable
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 Assertions

Constant Summary

Runnable - Inherited

SIGNALS

Assertions - Included

E, UNDEFINED

Class Attribute Summary

Class Method Summary

Guard - Extended

jruby?

Is this running on jruby?

maglev?

Is this running on maglev?

mri?

Is this running on mri?

rubinius?

Is this running on rubinius?

windows?

Is this running on windows?

Runnable - Inherited

.methods_matching

Returns all instance methods matching the pattern re.

.run

Responsible for running all runnable methods in a given class, each in its own instance.

.run_one_method

Runs a single method and has the reporter record the result.

.runnable_methods

Each subclass of Runnable is responsible for overriding this method to return all runnable methods.

.runnables

Returns all subclasses of Runnable.

.inherited, .new, .on_signal, .reset, .with_info_handler

Instance Attribute Summary

Assertions - Included

#skipped?

Was this testcase skipped? Meant for #teardown.

Runnable - Inherited

#assertions

Number of assertions executed in this run.

#failures

An assertion raised during the run, if any.

#name

Name of the run.

#name=

Set the name of the run.

#passed?

Did this run pass?

#skipped?

Was this run skipped? See #passed? for more information.

Instance Method Summary

Guard - Included

#jruby?

Is this running on jruby?

#maglev?

Is this running on maglev?

#mri?

Is this running on mri?

#rubinius?

Is this running on rubinius?

#windows?

Is this running on windows?

LifecycleHooks - Included

#after_setup

Runs before every test, after setup.

#after_teardown

Runs after every test, after teardown.

#before_setup

Runs before every test, before setup.

#before_teardown

Runs after every test, before teardown.

#setup

Runs before every test.

#teardown

Runs after every test.

Assertions - Included

#assert

Fails unless test is truthy.

#assert_empty

Fails unless obj is empty.

#assert_equal

Fails unless exp == act printing the difference between the two, if possible.

#assert_in_delta

For comparing Floats.

#assert_in_epsilon

For comparing Floats.

#assert_includes

Fails unless collection includes obj.

#assert_instance_of

Fails unless obj is an instance of cls.

#assert_kind_of

Fails unless obj is a kind of cls.

#assert_match

Fails unless matcher =~ obj.

#assert_mock

Assert that the mock verifies correctly.

#assert_nil

Fails unless obj is nil.

#assert_operator

For testing with binary operators.

#assert_output

Fails if stdout or stderr do not output the expected results.

#assert_predicate

For testing with predicates.

#assert_raises

Fails unless the block raises one of exp.

#assert_respond_to

Fails unless obj responds to meth.

#assert_same

Fails unless exp and act are #equal?

#assert_send

send_ary is a receiver, message and arguments.

#assert_silent

Fails if the block outputs anything to stderr or stdout.

#assert_throws

Fails unless the block throws sym

#capture_io

Captures $stdout and $stderr into strings:

#capture_subprocess_io

Captures $stdout and $stderr into strings, using Tempfile to ensure that subprocess IO is captured as well.

#diff

Returns a diff between exp and act.

#exception_details

Returns details for exception e

#flunk

Fails with msg

#message

Returns a proc that will output msg along with the default message.

#mu_pp

This returns a human-readable version of obj.

#mu_pp_for_diff

This returns a diff-able human-readable version of obj.

#pass

used for counting assertions.

#refute

Fails if test is truthy.

#refute_empty

Fails if obj is empty.

#refute_equal

Fails if exp == act.

#refute_in_delta

For comparing Floats.

#refute_in_epsilon

For comparing Floats.

#refute_includes

Fails if collection includes obj.

#refute_instance_of

Fails if obj is an instance of cls.

#refute_kind_of

Fails if obj is a kind of cls.

#refute_match

Fails if matcher =~ obj.

#refute_nil

Fails if obj is nil.

#refute_operator

Fails if o1 is not op o2.

#refute_predicate

For testing with predicates.

#refute_respond_to

Fails if obj responds to the message meth.

#refute_same

Fails if exp is the same (by object identity) as act.

#skip

Skips the current run.

#_synchronize

Runnable - Inherited

#result_code

Returns a single character string to print based on the result of the run.

#run

Runs a single method.

#failure, #marshal_dump, #marshal_load

Constructor Details

This class inherits a constructor from Minitest::Runnable

Class Attribute Details

.io_lock (rw)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 17

class << self; attr_accessor :io_lock; end

Class Method Details

.i_suck_and_my_tests_are_order_dependent!

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.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 26

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!

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.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 39

def self.make_my_diffs_pretty!
  require "pp"

  define_method :mu_pp, &:pretty_inspect
end

.parallelize_me!

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.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 50

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

.runnable_methods

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

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 60

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_order

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

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 78

def self.test_order
  :random
end

Instance Attribute Details

#error?Boolean (readonly)

Did this run error?

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 216

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

#passed?Boolean (readonly)

Did this run pass?

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

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 234

def passed?
  not self.failure
end

#skipped?Boolean (readonly)

Was this run skipped?

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 248

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

#time (rw)

The time it took to run this test.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 85

attr_accessor :time

Instance Method Details

#capture_exceptions

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 203

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

#location

The location identifier of this test.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 223

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

#marshal_dump

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 87

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

#marshal_load(ary)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 91

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

#result_code

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

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 241

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

#run

Runs a single test with setup/teardown hooks.

[ GitHub ]

  
# File 'lib/minitest/test.rb', line 101

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

#time_it

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 252

def time_it # :nodoc:
  t0 = Minitest.clock_time

  yield
ensure
  self.time = Minitest.clock_time - t0
end

#to_s

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 260

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)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/minitest/test.rb', line 268

def with_info_handler &block # :nodoc:
  t0 = Minitest.clock_time

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

  self.class.on_signal ::Minitest.info_signal, handler, &block
end