Class: MiniTest::Unit

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

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =
"1.4.2"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

Returns a new instance of Unit.



364
365
366
367
368
# File 'lib/minitest/unit.rb', line 364

def initialize
  @report = []
  @errors = @failures = @skips = 0
  @verbose = false
end

Instance Attribute Details

#assertion_countObject

Returns the value of attribute assertion_count.



319
320
321
# File 'lib/minitest/unit.rb', line 319

def assertion_count
  @assertion_count
end

#errorsObject

Returns the value of attribute errors.



318
319
320
# File 'lib/minitest/unit.rb', line 318

def errors
  @errors
end

#failuresObject

Returns the value of attribute failures.



318
319
320
# File 'lib/minitest/unit.rb', line 318

def failures
  @failures
end

#reportObject

Returns the value of attribute report.



318
319
320
# File 'lib/minitest/unit.rb', line 318

def report
  @report
end

#skipsObject

Returns the value of attribute skips.



318
319
320
# File 'lib/minitest/unit.rb', line 318

def skips
  @skips
end

#start_timeObject

Returns the value of attribute start_time.



320
321
322
# File 'lib/minitest/unit.rb', line 320

def start_time
  @start_time
end

#test_countObject

Returns the value of attribute test_count.



319
320
321
# File 'lib/minitest/unit.rb', line 319

def test_count
  @test_count
end

Class Method Details

.autorunObject



325
326
327
328
329
330
331
332
# File 'lib/minitest/unit.rb', line 325

def self.autorun
  at_exit {
    next if $! # don't run if there was an exception
    exit_code = MiniTest::Unit.new.run(ARGV)
    exit false if exit_code && exit_code != 0
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

.output=(stream) ⇒ Object



334
335
336
# File 'lib/minitest/unit.rb', line 334

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

Instance Method Details

#location(e) ⇒ Object



338
339
340
341
342
343
344
345
# File 'lib/minitest/unit.rb', line 338

def location e
  last_before_assertion = ""
  e.backtrace.reverse_each do |s|
    break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
    last_before_assertion = s
  end
  last_before_assertion.sub(/:in .*$/, '')
end

#puke(klass, meth, e) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/minitest/unit.rb', line 347

def puke klass, meth, e
  e = case e
      when MiniTest::Skip then
        @skips += 1
        "Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
      when MiniTest::Assertion then
        @failures += 1
        "Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
      else
        @errors += 1
        bt = MiniTest::filter_backtrace(e.backtrace).join("\n    ")
        "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n    #{bt}\n"
      end
  @report << e
  e[0, 1]
end

#run(args = []) ⇒ Object

Top level driver, controls all output and filtering.



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/minitest/unit.rb', line 373

def run args = []
  @verbose = args.delete('-v')

  filter = if args.first =~ /^(-n|--name)$/ then
             args.shift
             arg = args.shift
             arg =~ /\/(.*)\// ? Regexp.new($1) : arg
           else
             /./ # anything - ^test_ already filtered by #tests
           end

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

  start = Time.now
  run_test_suites filter

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

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

  @@out.puts

  status

  return failures + errors if @test_count > 0 # or return nil...
rescue Interrupt
  abort 'Interrupted'
end

#run_test_suites(filter = /./) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/minitest/unit.rb', line 410

def run_test_suites filter = /./
  @test_count, @assertion_count = 0, 0
  old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
  TestCase.test_suites.each do |suite|
    suite.test_methods.grep(filter).each do |test|
      inst = suite.new test
      inst._assertions = 0
      @@out.print "#{suite}##{test}: " if @verbose

      @start_time = Time.now
      result = inst.run(self)

      @@out.print "%.2f s: " % (Time.now - @start_time) if @verbose
      @@out.print result
      @@out.puts if @verbose
      @test_count += 1
      @assertion_count += inst._assertions
    end
  end
  @@out.sync = old_sync if @@out.respond_to? :sync=
  [@test_count, @assertion_count]
end

#status(io = @@out) ⇒ Object



405
406
407
408
# File 'lib/minitest/unit.rb', line 405

def status io = @@out
  format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
  io.puts format % [test_count, assertion_count, failures, errors, skips]
end