Class: MiniTest::Unit

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

Defined Under Namespace

Classes: TestCase

Constant Summary collapse

VERSION =

:nodoc:

"2.0.2"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

:nodoc:



707
708
709
710
711
# File 'lib/minitest/unit.rb', line 707

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

Instance Attribute Details

#assertion_countObject

:nodoc:



520
521
522
# File 'lib/minitest/unit.rb', line 520

def assertion_count
  @assertion_count
end

#errorsObject

:nodoc:



519
520
521
# File 'lib/minitest/unit.rb', line 519

def errors
  @errors
end

#failuresObject

:nodoc:



519
520
521
# File 'lib/minitest/unit.rb', line 519

def failures
  @failures
end

#helpObject

:nodoc:



522
523
524
# File 'lib/minitest/unit.rb', line 522

def help
  @help
end

#optionsObject



526
527
528
# File 'lib/minitest/unit.rb', line 526

def options
  @options ||= {}
end

#reportObject

:nodoc:



519
520
521
# File 'lib/minitest/unit.rb', line 519

def report
  @report
end

#runnerObject

Returns the value of attribute runner.



5
6
7
# File 'lib/minitest/benchmark.rb', line 5

def runner
  @runner
end

#skipsObject

:nodoc:



519
520
521
# File 'lib/minitest/unit.rb', line 519

def skips
  @skips
end

#start_timeObject

:nodoc:



521
522
523
# File 'lib/minitest/unit.rb', line 521

def start_time
  @start_time
end

#test_countObject

:nodoc:



520
521
522
# File 'lib/minitest/unit.rb', line 520

def test_count
  @test_count
end

#verboseObject

:nodoc:



523
524
525
# File 'lib/minitest/unit.rb', line 523

def verbose
  @verbose
end

Class Method Details

.after_testsObject

A simple hook allowing you to run a block of code after the tests are done. Eg:

MiniTest::Unit.after_tests { p $debugging_info }


539
540
541
# File 'lib/minitest/unit.rb', line 539

def self.after_tests
  at_exit { at_exit { yield } }
end

.autorunObject

Registers MiniTest::Unit to run tests at process exit



546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/minitest/unit.rb', line 546

def self.autorun
  at_exit {
    next if $! # don't run if there was an exception

    # the order here is important. The at_exit handler must be
    # installed before anyone else gets a chance to install their
    # own, that way we can be assured that our exit will be last
    # to run (at_exit stacks).
    exit_code = nil

    at_exit { exit false if exit_code && exit_code != 0 }

    exit_code = MiniTest::Unit.new.run ARGV
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

.outObject

Returns the stream to use for output.

DEPRECATED: use ::output instead.



575
576
577
578
# File 'lib/minitest/unit.rb', line 575

def self.out
  warn "::out deprecated, use ::output instead." if $VERBOSE
  output
end

.outputObject

Returns the stream to use for output.



566
567
568
# File 'lib/minitest/unit.rb', line 566

def self.output
  @@out
end

.output=(stream) ⇒ Object

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



584
585
586
# File 'lib/minitest/unit.rb', line 584

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

.pluginsObject

Return all plugins’ run methods (methods that start with “run_”).



591
592
593
594
595
# File 'lib/minitest/unit.rb', line 591

def self.plugins
  @@plugins ||= (["run_tests"] +
                 public_instance_methods(false).
                 grep(/^run_/).map { |s| s.to_s }).uniq
end

Instance Method Details

#_run_anything(type) ⇒ Object



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
# File 'lib/minitest/unit.rb', line 609

def _run_anything type
  suites = TestCase.send "#{type}_suites"
  return if suites.empty?

  start = Time.now

  puts
  puts "# Running #{type}s:"
  puts

  @test_count, @assertion_count = 0, 0
  sync = output.respond_to? :"sync=" # stupid emacs
  old_sync, output.sync = output.sync, true if sync

  results = _run_suites suites, type

  @test_count      = results.inject(0) { |sum, (tc, ac)| sum + tc }
  @assertion_count = results.inject(0) { |sum, (tc, ac)| sum + ac }

  output.sync = old_sync if sync

  t = Time.now - start

  puts
  puts
  puts "Finished #{type}s in %.6fs, %.4f tests/s, %.4f assertions/s." %
    [t, test_count / t, assertion_count / t]

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

  puts

  status
end

#_run_suite(suite, type) ⇒ Object



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/minitest/unit.rb', line 650

def _run_suite suite, type
  header = "#{type}_suite_header"
  puts send(header, suite) if respond_to? header

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

  assertions = suite.send("#{type}_methods").grep(filter).map { |method|
    inst = suite.new method
    inst._assertions = 0

    print "#{suite}##{method} = " if @verbose

    @start_time = Time.now
    result = inst.run self
    time = Time.now - @start_time

    print "%.2f s = " % time if @verbose
    print result
    puts if @verbose

    inst._assertions
  }

  return assertions.size, assertions.inject(0) { |sum, n| sum + n }
end

#_run_suites(suites, type) ⇒ Object



646
647
648
# File 'lib/minitest/unit.rb', line 646

def _run_suites suites, type
  suites.map { |suite| _run_suite suite, type }
end

#benchmark_suite_header(suite) ⇒ Object

:nodoc:



11
12
13
# File 'lib/minitest/benchmark.rb', line 11

def benchmark_suite_header suite # :nodoc:
  "\n#{suite}\t#{suite.bench_range.join("\t")}"
end

#location(e) ⇒ Object

:nodoc:



677
678
679
680
681
682
683
684
# File 'lib/minitest/unit.rb', line 677

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

#outputObject



597
598
599
# File 'lib/minitest/unit.rb', line 597

def output
  self.class.output
end

:nodoc:



605
606
607
# File 'lib/minitest/unit.rb', line 605

def print *a # :nodoc:
  output.print(*a)
end

#process_args(args = []) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/minitest/unit.rb', line 713

def process_args args = []
  options = {}
  orig_args = args.dup

  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
    orig_args -= args
  end

  unless options[:seed] then
    srand
    options[:seed] = srand % 0xFFFF
    orig_args << "--seed" << options[:seed].to_s
  end

  srand options[:seed]

  self.verbose = options[:verbose]
  @help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " "

  options
end

#puke(klass, meth, e) ⇒ Object

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



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/minitest/unit.rb', line 690

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

#puts(*a) ⇒ Object

:nodoc:



601
602
603
# File 'lib/minitest/unit.rb', line 601

def puts *a  # :nodoc:
  output.puts(*a)
end

#run(args = []) ⇒ Object

Top level driver, controls all output and filtering.



759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/minitest/unit.rb', line 759

def run args = []
  self.options = process_args args

  puts "Run options: #{help}"

  self.class.plugins.each do |plugin|
    send plugin
    break unless report.empty?
  end

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

#run_benchmarksObject

:nodoc:



7
8
9
# File 'lib/minitest/benchmark.rb', line 7

def run_benchmarks # :nodoc:
  _run_anything :benchmark
end

#run_testsObject

Runs test suites matching filter.



777
778
779
# File 'lib/minitest/unit.rb', line 777

def run_tests
  _run_anything :test
end

#status(io = self.output) ⇒ Object

Writes status to io



784
785
786
787
# File 'lib/minitest/unit.rb', line 784

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