Class: MiniTest::Unit

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

Overview

:nodoc:

Defined Under Namespace

Modules: Guard Classes: TestCase

Constant Summary collapse

VERSION =

:nodoc:

"2.11.1"
@@out =
$stdout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUnit

:nodoc:



869
870
871
872
873
# File 'lib/minitest/unit.rb', line 869

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

Instance Attribute Details

#assertion_countObject

:nodoc:



649
650
651
# File 'lib/minitest/unit.rb', line 649

def assertion_count
  @assertion_count
end

#errorsObject

:nodoc:



648
649
650
# File 'lib/minitest/unit.rb', line 648

def errors
  @errors
end

#failuresObject

:nodoc:



648
649
650
# File 'lib/minitest/unit.rb', line 648

def failures
  @failures
end

#helpObject

:nodoc:



651
652
653
# File 'lib/minitest/unit.rb', line 651

def help
  @help
end

#optionsObject

Lazy accessor for options.



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

def options
  @options ||= {}
end

#reportObject

:nodoc:



648
649
650
# File 'lib/minitest/unit.rb', line 648

def report
  @report
end

#skipsObject

:nodoc:



648
649
650
# File 'lib/minitest/unit.rb', line 648

def skips
  @skips
end

#start_timeObject

:nodoc:



650
651
652
# File 'lib/minitest/unit.rb', line 650

def start_time
  @start_time
end

#test_countObject

:nodoc:



649
650
651
# File 'lib/minitest/unit.rb', line 649

def test_count
  @test_count
end

#verboseObject

:nodoc:



652
653
654
# File 'lib/minitest/unit.rb', line 652

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 }


671
672
673
# File 'lib/minitest/unit.rb', line 671

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

.autorunObject

Registers MiniTest::Unit to run tests at process exit



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/minitest/unit.rb', line 678

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.



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

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

.outputObject

Returns the stream to use for output.



698
699
700
# File 'lib/minitest/unit.rb', line 698

def self.output
  @@out
end

.output=(stream) ⇒ Object

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



716
717
718
# File 'lib/minitest/unit.rb', line 716

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

.pluginsObject

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



740
741
742
743
744
# File 'lib/minitest/unit.rb', line 740

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

.runnerObject

Returns the MiniTest::Unit subclass instance that will be used to run the tests. A MiniTest::Unit instance is the default runner.



733
734
735
# File 'lib/minitest/unit.rb', line 733

def self.runner
  @@runner ||= self.new
end

.runner=(runner) ⇒ Object

Tells MiniTest::Unit to delegate to runner, an instance of a MiniTest::Unit subclass, when MiniTest::Unit#run is called.



724
725
726
# File 'lib/minitest/unit.rb', line 724

def self.runner= runner
  @@runner = runner
end

Instance Method Details

#_run(args = []) ⇒ Object

Top level driver, controls all output and filtering.



928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/minitest/unit.rb', line 928

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_anything(type) ⇒ Object

Runner for a given type (eg, test vs bench).



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/minitest/unit.rb', line 764

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, _)| sum + tc }
  @assertion_count = results.inject(0) { |sum, (_, 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

Run a single suite for a given type.



811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/minitest/unit.rb', line 811

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

Runs all the suites for a given type.



804
805
806
# File 'lib/minitest/unit.rb', line 804

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

#benchmark_suite_header(suite) ⇒ Object

:nodoc:



9
10
11
# File 'lib/minitest/benchmark.rb', line 9

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

#location(e) ⇒ Object

:nodoc:



838
839
840
841
842
843
844
845
# File 'lib/minitest/unit.rb', line 838

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

Return the IO for output.



749
750
751
# File 'lib/minitest/unit.rb', line 749

def output
  self.class.output
end

:nodoc:



757
758
759
# File 'lib/minitest/unit.rb', line 757

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

#process_args(args = []) ⇒ Object

:nodoc:



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/minitest/unit.rb', line 875

def process_args args = [] # :nodoc:
  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 (e.g. /foo/)" 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



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/minitest/unit.rb', line 851

def puke klass, meth, e
  e = case e
      when MiniTest::Skip then
        @skips += 1
        return "S" unless @verbose
        "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:



753
754
755
# File 'lib/minitest/unit.rb', line 753

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

#run(args = []) ⇒ Object

Begins the full test run. Delegates to runner‘s #_run method.



921
922
923
# File 'lib/minitest/unit.rb', line 921

def run args = []
  self.class.runner._run(args)
end

#run_benchmarksObject

:nodoc:



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

def run_benchmarks # :nodoc:
  _run_anything :benchmark
end

#run_testsObject

Runs test suites matching filter.



946
947
948
# File 'lib/minitest/unit.rb', line 946

def run_tests
  _run_anything :test
end

#status(io = self.output) ⇒ Object

Writes status to io



953
954
955
956
# File 'lib/minitest/unit.rb', line 953

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