Class: MiniTest::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/test/unit.rb

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =
"1.1.0"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

Returns a new instance of Unit.



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

def initialize
  @report = []
  @results = {:errors => 0, :failures => 0}
end

Instance Attribute Details

#reportObject (readonly)

Returns the value of attribute report.



71
72
73
# File 'lib/test/unit.rb', line 71

def report
  @report
end

Class Method Details

.autotestObject



79
80
81
# File 'lib/test/unit.rb', line 79

def self.autotest
  self.new.autotest
end

.location_of_failure(e) ⇒ Object



87
88
89
90
91
# File 'lib/test/unit.rb', line 87

def self.location_of_failure(e)
  e.backtrace.find { |s|
    s !~ /in .(assert|flunk)/
  }.sub(/:in .*$/, '')
end

.output=(stream) ⇒ Object



75
76
77
# File 'lib/test/unit.rb', line 75

def self.output= stream
  @@out = stream
end

.tests_from(klass) ⇒ Object



83
84
85
# File 'lib/test/unit.rb', line 83

def self.tests_from(klass)
  klass.public_instance_methods(true).grep(/^test/).sort
end

Instance Method Details

#autotestObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/test/unit.rb', line 112

def autotest
  @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"

  timestamp_before_test_run = Time.now
  test_count, assertion_count = run_test_suites

  @@out.puts
  @@out.puts "Finished in #{'%.6f' % (Time.now - timestamp_before_test_run)} seconds."

  @report.each_with_index do |msg, i|
    @@out.puts "\n%3d) %s" % [i + 1, msg]
  end

  @@out.puts
  @@out.puts "%d tests, %d assertions, %d failures, %d errors" % [test_count, assertion_count, failures, errors]

  return failures + errors
end

#errorsObject



163
164
165
# File 'lib/test/unit.rb', line 163

def errors
  @results[:errors]
end

#failuresObject



159
160
161
# File 'lib/test/unit.rb', line 159

def failures
  @results[:failures]
end

#puke(klass, meth, e) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/test/unit.rb', line 93

def puke(klass, meth, e)
  if MiniTest::Assertion === e then
    @results[:failures] += 1
    loc = self.class.location_of_failure(e)
    @report << "Failure:\n#{meth}(#{klass}) [#{loc}]:\n#{e.message}\n"
    'F'
  else
    @results[:errors] += 1
    bt = filter_backtrace(e.backtrace).join("\n    ")
    @report << "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n    #{bt}\n"
    'E'
  end
end

#run_test_suitesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/test/unit.rb', line 131

def run_test_suites
  test_count, assertion_count = 0, 0
  TestCase.test_suites.each do |klass|
    inst = klass.new
    inst._assertions = 0
    tests = self.class.tests_from(klass)
    tests.each do |meth|
      result = '.'
      begin
        inst.setup
        inst.__send__ meth
      rescue Exception => e
        result = puke(klass, meth, e)
      ensure
        begin
          inst.teardown
        rescue Exception => e
          result = puke(klass, meth, e)
        end
      end
      @@out.print result
    end
    test_count += tests.size
    assertion_count += inst._assertions
  end
  [test_count, assertion_count]
end