Class: Minitest::Test
| Relationships & Source Files | |
| Namespace Children | |
|
Modules:
| |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
|
|
|
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
-
PASSTHROUGH_EXCEPTIONS =
Internal use only
# File 'lib/minitest/test.rb', line 15[NoMemoryError, SignalException, SystemExit]
-
SETUP_METHODS =
Internal use only
# File 'lib/minitest/test.rb', line 17%w[ before_setup setup after_setup ] -
TEARDOWN_METHODS =
Internal use only
# File 'lib/minitest/test.rb', line 19%w[ before_teardown teardown after_teardown ]
Runnable - Inherited
Reportable - Included
Assertions - Included
Class Attribute Summary
- .io_lock rw Internal use only
Class Method Summary
-
.abstract_test_case!(klass = self)
Declare the current class an abstract test case that doesn't run tests except in subclasses.
-
.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.
-
.make_my_diffs_pretty!
Make diffs for this
Testusepretty_inspectso that diff in Assertions#assert_equal can have more details. -
.parallelize_me!
Call this at the top of your tests (inside the
Testsubclass ordescribeblock) when you want to run your tests in parallel. -
.runnable_methods
Returns all public instance methods starting with "test_".
Guard - Extended
| jruby? | Is this running on jruby? |
| mri? | Is this running on mri? |
| osx? | Is this running on macOS? |
| windows? | Is this running on windows? |
Runnable - Inherited
| .filter_runnable_methods | Returns an array of filtered .runnable_methods. |
| .methods_matching | Returns all public instance methods matching the pattern |
| .run_order | Defines the order to run tests (:random by default). |
| .runnable_methods | Each subclass of |
| .runnables | Returns all subclasses of |
| .inherited, .new, .on_signal, .reset, .with_info_handler | |
Instance Attribute Summary
Assertions - Included
| #skipped? | Was this testcase skipped? Meant for |
Reportable - Included
Runnable - Inherited
| #assertions | Number of assertions executed in this run. |
| #failures | An assertion raised during the run, if any. |
| #metadata | Metadata you attach to the test results that get sent to the reporter. |
| #metadata= | Sets metadata, mainly used for Result.from. |
| #metadata? | Returns true if metadata exists. |
| #name | Name of the run. |
| #name= | Set the name of the run. |
| #passed? | Did this run pass? |
| #skipped? | Was this run skipped? See |
| #time | The time it took to run. |
Instance Method Summary
-
#run
Runs a single test with setup/teardown hooks.
- #capture_exceptions Internal use only
- #neuter_exception(e) Internal use only
- #new_exception(klass, msg, bt, kill = false) Internal use only
- #sanitize_exception(e) Internal use only
Guard - Included
| #jruby? | Is this running on jruby? |
| #mri? | Is this running on mri? |
| #osx? | Is this running on macOS? |
| #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 |
| #assert_empty | Fails unless |
| #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 |
| #assert_instance_of | Fails unless |
| #assert_kind_of | Fails unless |
| #assert_match | Fails unless |
| #assert_nil | Fails unless |
| #assert_operator | For testing with binary operators. |
| #assert_output | Fails if stdout or stderr do not output the expected results. |
| #assert_path_exists | Fails unless |
| #assert_pattern | For testing with pattern matching (only supported with Ruby 3.0 and later). |
| #assert_predicate | For testing with predicates. |
| #assert_raises | Fails unless the block raises one of |
| #assert_respond_to | Fails unless |
| #assert_same | Fails unless |
| #assert_silent | Fails if the block outputs anything to stderr or stdout. |
| #assert_throws | Fails unless the block throws |
| #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 |
| #exception_details | Returns details for exception |
| #fail_after | Fails after a given date (in the local time zone). |
| #flunk | Fails with |
| #message | Returns a proc that delays generation of an output message. |
| #mu_pp | This returns a human-readable version of |
| #mu_pp_for_diff | This returns a diff-able more human-readable version of |
| #pass | used for counting assertions. |
| #refute | Fails if |
| #refute_empty | Fails if |
| #refute_equal | Fails if exp == act. |
| #refute_in_delta | For comparing Floats. |
| #refute_in_epsilon | For comparing Floats. |
| #refute_includes | Fails if |
| #refute_instance_of | Fails if |
| #refute_kind_of | Fails if |
| #refute_match | Fails if |
| #refute_nil | Fails if |
| #refute_operator | Fails if |
| #refute_path_exists | Fails if |
| #refute_pattern | For testing with pattern matching (only supported with Ruby 3.0 and later). |
| #refute_predicate | For testing with predicates. |
| #refute_respond_to | Fails if |
| #refute_same | Fails if |
| #skip | Skips the current run. |
| #skip_until | Skips the current run until a given date (in the local time zone). |
| #things_to_diff | Returns things to diff [expect, butwas], or [nil, nil] if nothing to diff. |
| #_synchronize | |
Reportable - Included
| #location | The location identifier of this test. |
| #result_code | Returns ".", "F", or "E" based on the result of the run. |
| #class_name | |
Runnable - Inherited
| #result_code | Returns a single character string to print based on the result of the run. |
| #run | Runs a single method. |
| #failure, #time_it | |
Constructor Details
This class inherits a constructor from Minitest::Runnable
Class Attribute Details
.io_lock (rw)
# File 'lib/minitest/test.rb', line 22
class << self; attr_accessor :io_lock; end
Class Method Details
.abstract_test_case!(klass = self)
Declare the current class an abstract test case that doesn't run tests except in subclasses.
Example:
class AbstractTestCase < Minitest::Test
abstract_test_case!
attr_accessor :obj
def test_method
assert_equal 42, obj.method
end
end
class TestConcrete < AbstractTestCase
def setup = self.obj = Concrete.new
end
.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.
# File 'lib/minitest/test.rb', line 58
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!
Make diffs for this Test use pretty_inspect so that diff
in Assertions#assert_equal can have more details.
NOTE: this is much slower than the regular inspect but much more usable for complex objects.
Example:
class TestBlah < Minitest::Test
make_my_diffs_pretty!
def test_complex_object
exp = deep_complex_object
assert_equal exp, obj.actual # outputs clean diff
end
end
# File 'lib/minitest/test.rb', line 83
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 (inside the Test
subclass or describe block) when you want to run your tests in
parallel.
Example:
class TestZeroSideEffects < Minitest::Test
parallelize_me!
# ... tests ...
end
# File 'lib/minitest/test.rb', line 102
def self.parallelize_me! return unless Minitest.parallel_executor include Minitest::Parallel::Test extend Minitest::Parallel::Test::ClassMethods end
.runnable_methods
Returns all public instance methods starting with "test_". Based on
#run_order, the methods are either sorted, randomized
(default), or run in parallel.
Instance Method Details
#capture_exceptions
# File 'lib/minitest/test.rb', line 244
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)
# File 'lib/minitest/test.rb', line 261
def neuter_exception e # :nodoc: bt = e.backtrace msg = e..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)
# File 'lib/minitest/test.rb', line 272
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
#run
Runs a single test with setup/teardown hooks. Override this method to customize how the test is run on a class-by-class basis.
Example:
class TestBlah < Minitest::Test
def run
Dir.chdir tmp_dir do
super
end
end
end
# File 'lib/minitest/test.rb', line 142
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)
# File 'lib/minitest/test.rb', line 254
def sanitize_exception e # :nodoc: Marshal.dump e e # good: use as-is rescue neuter_exception e end