Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Includes:
Assertions, Util::BacktraceFilter
Defined in:
lib/test/unit/testcase.rb

Overview

Ties everything together. If you subclass and add your own test methods, it takes care of making them into tests and wrapping those tests into a suite. It also does the nitty-gritty of actually running an individual test and collecting its results into a Test::Unit::TestResult object.

Constant Summary collapse

STARTED =
name + "::STARTED"
FINISHED =
name + "::FINISHED"
PASSTHROUGH_EXCEPTIONS =

These exceptions are not caught by #run.

[NoMemoryError, SignalException, Interrupt,
SystemExit]

Constants included from Util::BacktraceFilter

Util::BacktraceFilter::TESTUNIT_FILE_SEPARATORS, Util::BacktraceFilter::TESTUNIT_PREFIX, Util::BacktraceFilter::TESTUNIT_RB_FILE

Constants included from Assertions

Assertions::UncaughtThrow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::BacktraceFilter

#filter_backtrace

Methods included from Assertions

#assert, #assert_block, #assert_equal, #assert_in_delta, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_no_match, #assert_not_equal, #assert_not_nil, #assert_not_same, #assert_nothing_raised, #assert_nothing_thrown, #assert_operator, #assert_raise, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_throws, #build_message, #flunk, use_pp=

Constructor Details

#initialize(test_method_name) ⇒ TestCase

Creates a new instance of the fixture for running the test represented by test_method_name.



39
40
41
42
43
44
45
46
47
# File 'lib/test/unit/testcase.rb', line 39

def initialize(test_method_name)
  unless(respond_to?(test_method_name) and
         (method(test_method_name).arity == 0 ||
          method(test_method_name).arity == -1))
    throw :invalid_test
  end
  @method_name = test_method_name
  @test_passed = true
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



26
27
28
# File 'lib/test/unit/testcase.rb', line 26

def method_name
  @method_name
end

Class Method Details

.suiteObject

Rolls up all of the test* methods in the fixture into one suite, creating a new instance of the fixture for each method.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/test/unit/testcase.rb', line 52

def self.suite
  method_names = public_instance_methods(true)
  tests = method_names.delete_if {|method_name| method_name !~ /^test./}
  suite = TestSuite.new(name)
  tests.sort.each do
    |test|
    catch(:invalid_test) do
      suite << new(test)
    end
  end
  if (suite.empty?)
    catch(:invalid_test) do
      suite << new("default_test")
    end
  end
  return suite
end

Instance Method Details

#==(other) ⇒ Object

It’s handy to be able to compare TestCase instances.



153
154
155
156
157
# File 'lib/test/unit/testcase.rb', line 153

def ==(other)
  return false unless(other.kind_of?(self.class))
  return false unless(@method_name == other.method_name)
  self.class == other.class
end

#default_testObject



108
109
110
# File 'lib/test/unit/testcase.rb', line 108

def default_test
  flunk("No tests were specified")
end

#nameObject

Returns a human-readable name for the specific test that this instance of TestCase represents.



143
144
145
# File 'lib/test/unit/testcase.rb', line 143

def name
  "#{@method_name}(#{self.class.name})"
end

#run(result) {|STARTED, name| ... } ⇒ Object

Runs the individual test method represented by this instance of the fixture, collecting statistics, failures and errors in result.

Yields:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/test/unit/testcase.rb', line 73

def run(result)
  yield(STARTED, name)
  @_result = result
  begin
    setup
    __send__(@method_name)
  rescue AssertionFailedError => e
    add_failure(e.message, e.backtrace)
  rescue Exception
    raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
    add_error($!)
  ensure
    begin
      teardown
    rescue AssertionFailedError => e
      add_failure(e.message, e.backtrace)
    rescue Exception
      raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
      add_error($!)
    end
  end
  result.add_run
  yield(FINISHED, name)
end

#setupObject

Called before every test method runs. Can be used to set up fixture information.



100
101
# File 'lib/test/unit/testcase.rb', line 100

def setup
end

#sizeObject



120
121
122
# File 'lib/test/unit/testcase.rb', line 120

def size
  1
end

#teardownObject

Called after every test method runs. Can be used to tear down fixture information.



105
106
# File 'lib/test/unit/testcase.rb', line 105

def teardown
end

#to_sObject

Overridden to return #name.



148
149
150
# File 'lib/test/unit/testcase.rb', line 148

def to_s
  name
end