Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • minitest/unit.rb

MiniTest::Unit::TestCase

Subclass TestCase to create your own tests. Typically you'll want a TestCase subclass per implementation class.

See MiniTest::Assertions

Public Class Methods

add_setup_hook(arg=nil, &block) click to toggle source

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
            
add_teardown_hook(arg=nil, &block) click to toggle source

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
            
i_suck_and_my_tests_are_order_dependent!() click to toggle source

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
            

Public Instance Methods

io() click to toggle source
 
               # File minitest/unit.rb, line 977
def io
  @__io__ = true
  MiniTest::Unit.output
end
            
io?() click to toggle source
 
               # File minitest/unit.rb, line 982
def io?
  @__io__
end
            
passed?() click to toggle source

Returns true if the test passed.

 
               # File minitest/unit.rb, line 1034
def passed?
  @passed
end
            
run(runner) click to toggle source

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
            
setup() click to toggle source

Runs before every test. Use this to refactor test initialization.

 
               # File minitest/unit.rb, line 1041
def setup; end
            
teardown() click to toggle source

Runs after every test. Use this to refactor test cleanup.

 
               # File minitest/unit.rb, line 1046
def teardown; end