Class: Minitest::Test
- Extended by:
- Guard
- Includes:
- Assertions, Guard, LifecycleHooks
- Defined in:
- lib/minitest/test.rb,
lib/minitest/hell.rb,
lib/minitest/parallel_each.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, # :nodoc: Interrupt, SystemExit]
Constants included from Assertions
Instance Attribute Summary collapse
-
#time ⇒ Object
The time it took to run this test.
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.
-
.old_test_order ⇒ Object
:nodoc:.
-
.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_”.
-
.synchronize ⇒ Object
:nodoc:.
-
.test_order ⇒ Object
Defines the order to run tests (:random by default).
Instance Method Summary collapse
-
#capture_exceptions ⇒ Object
LifecycleHooks.
- #capture_io(&b) ⇒ Object
- #capture_subprocess_io(&b) ⇒ Object
-
#error? ⇒ Boolean
Did this run error?.
-
#location ⇒ Object
The location identifier of this test.
-
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load(ary) ⇒ Object
:nodoc:.
-
#passed? ⇒ Boolean
Did this run pass?.
-
#result_code ⇒ Object
Returns “.”, “F”, or “E” based on the result of the run.
-
#run ⇒ Object
Runs a single test with setup/teardown hooks.
- #simple_capture_io ⇒ Object
- #simple_capture_subprocess_io ⇒ Object
-
#skipped? ⇒ Boolean
Was this run skipped?.
-
#time_it ⇒ Object
:nodoc:.
-
#to_s ⇒ Object
:nodoc:.
-
#with_info_handler(&block) ⇒ Object
:nodoc:.
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
#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, #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
#after_last_method, #before_first_method, check_failures, #failure, inherited, #initialize, methods_matching, #name, #name=, on_signal, reset, run, runnables, with_info_handler
Constructor Details
This class inherits a constructor from Minitest::Runnable
Instance Attribute Details
#time ⇒ Object
The time it took to run this test.
75 76 77 |
# File 'lib/minitest/test.rb', line 75 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.
22 23 24 25 26 27 |
# File 'lib/minitest/test.rb', line 22 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.
35 36 37 38 39 40 41 |
# File 'lib/minitest/test.rb', line 35 def self.make_my_diffs_pretty! require "pp" define_method :mu_pp do |o| o.pretty_inspect end end |
.old_test_order ⇒ Object
:nodoc:
5 |
# File 'lib/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.
48 49 50 51 52 53 54 55 |
# File 'lib/minitest/test.rb', line 48 def self.parallelize_me! require "minitest/parallel_each" class << self undef_method :test_order if method_defined? :test_order define_method :test_order do :parallel end end end |
.runnable_methods ⇒ Object
Returns all instance methods starting with “test_”.
60 61 62 |
# File 'lib/minitest/test.rb', line 60 def self.runnable_methods methods_matching(/^test_/) end |
.synchronize ⇒ Object
:nodoc:
63 64 65 66 67 68 69 |
# File 'lib/minitest/parallel_each.rb', line 63 def self.synchronize # :nodoc: if @mutex then # see parallel_each.rb @mutex.synchronize { yield } else yield 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.
7 8 9 |
# File 'lib/minitest/hell.rb', line 7 def test_order # :nodoc: :parallel end |
Instance Method Details
#capture_exceptions ⇒ Object
LifecycleHooks
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/minitest/test.rb', line 191 def capture_exceptions # :nodoc: begin yield rescue *PASSTHROUGH_EXCEPTIONS raise rescue Assertion => e self.failures << e rescue Exception => e self.failures << UnexpectedError.new(e) end end |
#capture_io(&b) ⇒ Object
73 74 75 76 77 |
# File 'lib/minitest/parallel_each.rb', line 73 def capture_io(&b) Test.synchronize do simple_capture_io(&b) end end |
#capture_subprocess_io(&b) ⇒ Object
81 82 83 84 85 |
# File 'lib/minitest/parallel_each.rb', line 81 def capture_subprocess_io(&b) Test.synchronize do simple_capture_subprocess_io(&b) end end |
#error? ⇒ Boolean
Did this run error?
206 207 208 |
# File 'lib/minitest/test.rb', line 206 def error? self.failures.any? { |f| UnexpectedError === f } end |
#location ⇒ Object
The location identifier of this test.
213 214 215 216 |
# File 'lib/minitest/test.rb', line 213 def location loc = " [#{self.failure.location}]" unless passed? or error? "#{self.class}##{self.name}#{loc}" end |
#marshal_dump ⇒ Object
:nodoc:
77 78 79 |
# File 'lib/minitest/test.rb', line 77 def marshal_dump # :nodoc: super << self.time end |
#marshal_load(ary) ⇒ Object
:nodoc:
81 82 83 84 |
# File 'lib/minitest/test.rb', line 81 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.
224 225 226 |
# File 'lib/minitest/test.rb', line 224 def passed? not self.failure end |
#result_code ⇒ Object
Returns “.”, “F”, or “E” based on the result of the run.
231 232 233 |
# File 'lib/minitest/test.rb', line 231 def result_code self.failure and self.failure.result_code or "." end |
#run ⇒ Object
Runs a single test with setup/teardown hooks.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/minitest/test.rb', line 89 def run with_info_handler do time_it do capture_exceptions do before_setup; setup; after_setup self.send self.name end %w{ before_teardown teardown after_teardown }.each do |hook| capture_exceptions do self.send hook end end end end self # per contract end |
#simple_capture_io ⇒ Object
71 |
# File 'lib/minitest/parallel_each.rb', line 71 alias :simple_capture_io :capture_io |
#simple_capture_subprocess_io ⇒ Object
79 |
# File 'lib/minitest/parallel_each.rb', line 79 alias :simple_capture_subprocess_io :capture_subprocess_io |
#skipped? ⇒ Boolean
Was this run skipped?
238 239 240 |
# File 'lib/minitest/test.rb', line 238 def skipped? self.failure and Skip === self.failure end |
#time_it ⇒ Object
:nodoc:
242 243 244 245 246 247 248 |
# File 'lib/minitest/test.rb', line 242 def time_it # :nodoc: t0 = Time.now yield ensure self.time = Time.now - t0 end |
#to_s ⇒ Object
:nodoc:
250 251 252 253 254 255 256 |
# File 'lib/minitest/test.rb', line 250 def to_s # :nodoc: return location if passed? and not skipped? failures.map { |failure| "#{failure.result_label}:\n#{self.location}:\n#{failure.}\n" }.join "\n" end |
#with_info_handler(&block) ⇒ Object
:nodoc:
258 259 260 261 262 263 264 265 266 |
# File 'lib/minitest/test.rb', line 258 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 |