Class: MiniTest::Unit

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

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =

:nodoc:

"1.7.2"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

:nodoc:



568
569
570
571
572
# File 'lib/minitest/unit.rb', line 568

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

Instance Attribute Details

#assertion_countObject

:nodoc:



512
513
514
# File 'lib/minitest/unit.rb', line 512

def assertion_count
  @assertion_count
end

#errorsObject

:nodoc:



511
512
513
# File 'lib/minitest/unit.rb', line 511

def errors
  @errors
end

#failuresObject

:nodoc:



511
512
513
# File 'lib/minitest/unit.rb', line 511

def failures
  @failures
end

#reportObject

:nodoc:



511
512
513
# File 'lib/minitest/unit.rb', line 511

def report
  @report
end

#skipsObject

:nodoc:



511
512
513
# File 'lib/minitest/unit.rb', line 511

def skips
  @skips
end

#start_timeObject

:nodoc:



513
514
515
# File 'lib/minitest/unit.rb', line 513

def start_time
  @start_time
end

#test_countObject

:nodoc:



512
513
514
# File 'lib/minitest/unit.rb', line 512

def test_count
  @test_count
end

Class Method Details

.autorunObject

Registers MiniTest::Unit to run tests at process exit



521
522
523
524
525
526
527
528
# File 'lib/minitest/unit.rb', line 521

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

Sets MiniTest::Unit to write output to stream. $stdout is the default output



534
535
536
# File 'lib/minitest/unit.rb', line 534

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

Instance Method Details

#location(e) ⇒ Object

:nodoc:



538
539
540
541
542
543
544
545
# File 'lib/minitest/unit.rb', line 538

def location e # :nodoc:
  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

#process_args(args = []) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/minitest/unit.rb', line 574

def process_args args = []
  options = {}

  OptionParser.new do |opts|
    opts.banner  = 'minitest options:'
    opts.version = MiniTest::Unit::VERSION

    opts.on '-h', '--help', 'Display this help.' do
      puts opts
      exit
    end

    opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
      options[:seed] = m.to_i
    end

    opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
      options[:verbose] = true
    end

    opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
      options[:filter] = a
    end

    opts.parse args
  end

  options
end

#puke(klass, meth, e) ⇒ Object

Writes status for failed test meth in klass which finished with exception e



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/minitest/unit.rb', line 551

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.



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/minitest/unit.rb', line 607

def run args = []
  options = process_args args

  @verbose = options[:verbose]

  filter = options[:filter] || '/./'
  filter = Regexp.new $1 if filter and filter =~ /\/(.*)\//

  seed = options[:seed]
  unless seed then
    srand
    seed = srand % 0xFFFF
  end

  srand seed

  help = ["--seed", seed]
  help.push "--verbose" if @verbose
  help.push("--name", options[:filter].inspect) if options[:filter]

  @@out.puts "Test run options: #{help.join(" ")}"
  @@out.puts
  @@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

  @@out.puts

  @@out.puts "Test run options: #{help.join(" ")}"

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

#run_test_suites(filter = /./) ⇒ Object

Runs test suites matching filter



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/minitest/unit.rb', line 665

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

Writes status to io



657
658
659
660
# File 'lib/minitest/unit.rb', line 657

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