Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more
Object
Subclass TestCase to create your own tests. Typically you’ll want a TestCase subclass per implementation class.
Adds a block of code that will be executed before every TestCase is run. Equivalent to setup,
but usable multiple times and without re-opening any classes.
All of the setup hooks will run in order after the setup
method, if one is defined.
The argument can be any object that responds to call or a block. That means that this call,
MiniTest::TestCase.add_setup_hook { puts "foo" }
… is equivalent to:
module MyTestSetup def call puts "foo" end end MiniTest::TestCase.add_setup_hook MyTestSetup
The blocks passed to add_setup_hook take an optional parameter
that will be the TestCase instance that is
executing the block.
# File minitest/unit.rb, line 1081
def self.add_setup_hook arg=nil, &block
hook = arg || block
@setup_hooks << hook
end
Adds a block of code that will be executed after every TestCase is run. Equivalent to
teardown, but usable multiple times and without re-opening any
classes.
All of the teardown hooks will run in reverse order after the
teardown method, if one is defined.
The argument can be any object that responds to call or a block. That means that this call,
MiniTest::TestCase.add_teardown_hook { puts "foo" }
… is equivalent to:
module MyTestTeardown def call puts "foo" end end MiniTest::TestCase.add_teardown_hook MyTestTeardown
The blocks passed to add_teardown_hook take an optional
parameter that will be the TestCase instance
that is executing the block.
# File minitest/unit.rb, line 1130
def self.add_teardown_hook arg=nil, &block
hook = arg || block
@teardown_hooks << hook
end
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.
# File minitest/unit.rb, line 997
def self.i_suck_and_my_tests_are_order_dependent!
class << self
define_method :test_order do :alpha end
end
end
# File minitest/unit.rb, line 977
def io
@__io__ = true
MiniTest::Unit.output
end
Returns true if the test passed.
# File minitest/unit.rb, line 1034
def passed?
@passed
end
Runs the tests reporting the status to runner
# File minitest/unit.rb, line 937
def run runner
trap "INFO" do
time = runner.start_time ? Time.now - runner.start_time : 0
warn "%s#%s %.2fs" % [self.class, self.__name__, time]
runner.status $stderr
end if SUPPORTS_INFO_SIGNAL
result = ""
begin
@passed = nil
self.setup
self.run_setup_hooks
self.__send__ self.__name__
result = "." unless io?
@passed = true
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue Exception => e
@passed = false
result = runner.puke self.class, self.__name__, e
ensure
begin
self.run_teardown_hooks
self.teardown
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue Exception => e
result = runner.puke self.class, self.__name__, e
end
trap 'INFO', 'DEFAULT' if SUPPORTS_INFO_SIGNAL
end
result
end
Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.
If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.
If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.
If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.