Class: Minitest::Test
- 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
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::E, Assertions::UNDEFINED
Constants inherited from Runnable
Class Attribute Summary collapse
-
.io_lock ⇒ Object
Returns the value of attribute io_lock.
Attributes inherited from Runnable
Class Method Summary collapse
-
.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.
-
.make_my_diffs_pretty! ⇒ Object
Make diffs for this Test use #pretty_inspect so that diff in assert_equal can have more details.
-
.parallelize_me! ⇒ Object
Call this at the top of your tests when you want to run your tests in parallel.
-
.runnable_methods ⇒ Object
Returns all instance methods starting with “test_”.
-
.test_order ⇒ Object
Defines the order to run tests (:random by default).
Instance Method Summary collapse
-
#capture_exceptions ⇒ Object
LifecycleHooks.
-
#class_name ⇒ Object
:nodoc:.
-
#neuter_exception(e) ⇒ Object
:nodoc:.
-
#new_exception(klass, msg, bt, kill = false) ⇒ Object
:nodoc:.
-
#run ⇒ Object
Runs a single test with setup/teardown hooks.
-
#sanitize_exception(e) ⇒ Object
:nodoc:.
-
#with_info_handler(&block) ⇒ Object
:nodoc:.
Methods included from Guard
jruby?, maglev?, mri?, osx?, rubinius?, windows?
Methods included from LifecycleHooks
#after_setup, #after_teardown, #before_setup, #before_teardown, #setup, #teardown
Methods included from Reportable
#error?, #location, #passed?, #result_code, #skipped?
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_mock, #assert_nil, #assert_operator, #assert_output, #assert_path_exists, #assert_pattern, #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, #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 inherited from Runnable
#failure, inherited, #initialize, #marshal_dump, #marshal_load, methods_matching, #name, #name=, on_signal, #passed?, reset, #result_code, run, run_one_method, runnables, #skipped?, #time_it, with_info_handler
Constructor Details
This class inherits a constructor from Minitest::Runnable
Class Attribute Details
.io_lock ⇒ Object
Returns the value of attribute io_lock.
26 27 28 |
# File 'lib/minitest/test.rb', line 26 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.
35 36 37 38 39 40 |
# File 'lib/minitest/test.rb', line 35 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.
48 49 50 51 52 |
# File 'lib/minitest/test.rb', line 48 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 when you want to run your tests in parallel. In doing so, you’re admitting that you rule and your tests are awesome.
59 60 61 62 |
# File 'lib/minitest/test.rb', line 59 def self.parallelize_me! include Minitest::Parallel::Test extend Minitest::Parallel::Test::ClassMethods end |
.runnable_methods ⇒ Object
Returns all instance methods starting with “test_”. Based on #test_order, the methods are either sorted, randomized (default), or run in parallel.
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/minitest/test.rb', line 69 def self.runnable_methods methods = methods_matching(/^test_/) case self.test_order when :random, :parallel then srand Minitest.seed methods.sort.shuffle when :alpha, :sorted then methods.sort else raise "Unknown test_order: #{self.test_order.inspect}" end end |
.test_order ⇒ Object
Defines the order to run tests (:random by default). Override this or use a convenience method to change it for your tests.
87 88 89 |
# File 'lib/minitest/test.rb', line 87 def self.test_order :random end |
Instance Method Details
#capture_exceptions ⇒ Object
LifecycleHooks
198 199 200 201 202 203 204 205 206 |
# File 'lib/minitest/test.rb', line 198 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 |
#class_name ⇒ Object
:nodoc:
15 16 17 |
# File 'lib/minitest/test.rb', line 15 def class_name # :nodoc: self.class.name # for Minitest::Reportable end |
#neuter_exception(e) ⇒ Object
:nodoc:
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/minitest/test.rb', line 215 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) ⇒ Object
:nodoc:
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/minitest/test.rb', line 226 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 ⇒ Object
Runs a single test with setup/teardown hooks.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/minitest/test.rb', line 94 def run with_info_handler do 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 end Result.from self # per contract end |
#sanitize_exception(e) ⇒ Object
:nodoc:
208 209 210 211 212 213 |
# File 'lib/minitest/test.rb', line 208 def sanitize_exception e # :nodoc: Marshal.dump e e # good: use as-is rescue neuter_exception e end |
#with_info_handler(&block) ⇒ Object
:nodoc:
240 241 242 243 244 245 246 247 248 |
# File 'lib/minitest/test.rb', line 240 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 |