Class: MiniTest::Unit

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

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =
"1.3.0"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

Returns a new instance of Unit.



350
351
352
353
354
# File 'lib/minitest/unit.rb', line 350

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

Instance Attribute Details

#assertion_countObject

Returns the value of attribute assertion_count.



310
311
312
# File 'lib/minitest/unit.rb', line 310

def assertion_count
  @assertion_count
end

#errorsObject

Returns the value of attribute errors.



309
310
311
# File 'lib/minitest/unit.rb', line 309

def errors
  @errors
end

#failuresObject

Returns the value of attribute failures.



309
310
311
# File 'lib/minitest/unit.rb', line 309

def failures
  @failures
end

#reportObject

Returns the value of attribute report.



309
310
311
# File 'lib/minitest/unit.rb', line 309

def report
  @report
end

#skipsObject

Returns the value of attribute skips.



309
310
311
# File 'lib/minitest/unit.rb', line 309

def skips
  @skips
end

#test_countObject

Returns the value of attribute test_count.



310
311
312
# File 'lib/minitest/unit.rb', line 310

def test_count
  @test_count
end

Class Method Details

.autorunObject



315
316
317
318
319
320
321
# File 'lib/minitest/unit.rb', line 315

def self.autorun
  at_exit {
    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



323
324
325
# File 'lib/minitest/unit.rb', line 323

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

Instance Method Details

#location(e) ⇒ Object



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

def location e
  e.backtrace.find { |s|
    s !~ /in .(assert|refute|flunk|pass|fail|raise)/
  }.sub(/:in .*$/, '')
end

#puke(klass, meth, e) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/minitest/unit.rb', line 333

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.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/minitest/unit.rb', line 359

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

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

  return failures + errors if @test_count > 0 # or return nil...
end

#run_test_suites(filter = /./) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/minitest/unit.rb', line 390

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

      t = Time.now if @verbose
      result = inst.run(self)

      @@out.print "%.2f s: " % (Time.now - t) 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