Class: MiniTest::Unit::TestCase

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/minitest/unit.rb

Overview

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

Direct Known Subclasses

Spec

Constant Summary collapse

PASSTHROUGH_EXCEPTIONS =
[NoMemoryError, SignalException,
Interrupt, SystemExit]
SUPPORTS_INFO_SIGNAL =

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#_assertions, #_assertions=, #assert, #assert_block, #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_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #exception_details, #flunk, #message, #mu_pp, #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_respond_to, #refute_same, #skip

Constructor Details

#initialize(name) ⇒ TestCase

:nodoc:



734
735
736
737
# File 'lib/minitest/unit.rb', line 734

def initialize name # :nodoc:
  @__name__ = name
  @passed = nil
end

Instance Attribute Details

#__name__Object (readonly)

:nodoc:



693
694
695
# File 'lib/minitest/unit.rb', line 693

def __name__
  @__name__
end

Class Method Details

.inherited(klass) ⇒ Object

:nodoc:



745
746
747
# File 'lib/minitest/unit.rb', line 745

def self.inherited klass # :nodoc:
  @@test_suites[klass] = true
end

.resetObject

:nodoc:



739
740
741
# File 'lib/minitest/unit.rb', line 739

def self.reset # :nodoc:
  @@test_suites = {}
end

.test_methodsObject

:nodoc:



762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/minitest/unit.rb', line 762

def self.test_methods # :nodoc:
  methods = public_instance_methods(true).grep(/^test/).map { |m| m.to_s }

  case self.test_order
  when :random then
    max = methods.size
    methods.sort.sort_by { rand(max) }
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown test_order: #{self.test_order.inspect}"
  end
end

.test_orderObject

Defines test order and is subclassable. Defaults to :random but can be overridden to return :alpha if your tests are order dependent (read: weak).



754
755
756
# File 'lib/minitest/unit.rb', line 754

def self.test_order
  :random
end

.test_suitesObject

:nodoc:



758
759
760
# File 'lib/minitest/unit.rb', line 758

def self.test_suites # :nodoc:
  @@test_suites.keys.sort_by { |ts| ts.name }
end

Instance Method Details

#passed?Boolean

Returns true if the test passed.

Returns:

  • (Boolean)


779
780
781
# File 'lib/minitest/unit.rb', line 779

def passed?
  @passed
end

#run(runner) ⇒ Object

Runs the tests reporting the status to runner



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/minitest/unit.rb', line 703

def run runner
  trap 'INFO' do
    warn '%s#%s %.2fs' % [self.class, self.__name__,
      (Time.now - runner.start_time)]
    runner.status $stderr
  end if SUPPORTS_INFO_SIGNAL

  result = '.'
  begin
    @passed = nil
    self.setup
    self.__send__ self.__name__
    @passed = true
  rescue *PASSTHROUGH_EXCEPTIONS
    raise
  rescue Exception => e
    @passed = false
    result = runner.puke(self.class, self.__name__, e)
  ensure
    begin
      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

#setupObject

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



786
# File 'lib/minitest/unit.rb', line 786

def setup; end

#teardownObject

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



791
# File 'lib/minitest/unit.rb', line 791

def teardown; end