Class: MiniTest::Unit
- Defined in:
- lib/minitest/unit.rb,
lib/minitest/benchmark.rb
Defined Under Namespace
Classes: TestCase
Constant Summary collapse
- VERSION =
:nodoc:
"2.2.1"
- @@out =
$stdout
Instance Attribute Summary collapse
-
#assertion_count ⇒ Object
:nodoc:.
-
#errors ⇒ Object
:nodoc:.
-
#failures ⇒ Object
:nodoc:.
-
#help ⇒ Object
:nodoc:.
- #options ⇒ Object
-
#report ⇒ Object
:nodoc:.
-
#runner ⇒ Object
Returns the value of attribute runner.
-
#skips ⇒ Object
:nodoc:.
-
#start_time ⇒ Object
:nodoc:.
-
#test_count ⇒ Object
:nodoc:.
-
#verbose ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.after_tests ⇒ Object
A simple hook allowing you to run a block of code after the tests are done.
-
.autorun ⇒ Object
Registers MiniTest::Unit to run tests at process exit.
-
.out ⇒ Object
Returns the stream to use for output.
-
.output ⇒ Object
Returns the stream to use for output.
-
.output=(stream) ⇒ Object
Sets MiniTest::Unit to write output to
stream
. -
.plugins ⇒ Object
Return all plugins’ run methods (methods that start with “run_”).
-
.runner ⇒ Object
Returns the MiniTest::Unit subclass instance that will be used to run the tests.
-
.runner=(runner) ⇒ Object
Tells MiniTest::Unit to delegate to
runner
, an instance of a MiniTest::Unit subclass, when MiniTest::Unit#run is called.
Instance Method Summary collapse
-
#_run(args = []) ⇒ Object
Top level driver, controls all output and filtering.
- #_run_anything(type) ⇒ Object
- #_run_suite(suite, type) ⇒ Object
- #_run_suites(suites, type) ⇒ Object
-
#benchmark_suite_header(suite) ⇒ Object
:nodoc:.
-
#initialize ⇒ Unit
constructor
:nodoc:.
-
#location(e) ⇒ Object
:nodoc:.
- #output ⇒ Object
-
#print(*a) ⇒ Object
:nodoc:.
- #process_args(args = []) ⇒ Object
-
#puke(klass, meth, e) ⇒ Object
Writes status for failed test
meth
inklass
which finished with exceptione
. -
#puts(*a) ⇒ Object
:nodoc:.
-
#run(args = []) ⇒ Object
Begins the full test run.
-
#run_benchmarks ⇒ Object
:nodoc:.
-
#run_tests ⇒ Object
Runs test suites matching
filter
. -
#status(io = self.output) ⇒ Object
Writes status to
io
.
Constructor Details
#initialize ⇒ Unit
:nodoc:
824 825 826 827 828 |
# File 'lib/minitest/unit.rb', line 824 def initialize # :nodoc: @report = [] @errors = @failures = @skips = 0 @verbose = false end |
Instance Attribute Details
#assertion_count ⇒ Object
:nodoc:
619 620 621 |
# File 'lib/minitest/unit.rb', line 619 def assertion_count @assertion_count end |
#failures ⇒ Object
:nodoc:
618 619 620 |
# File 'lib/minitest/unit.rb', line 618 def failures @failures end |
#options ⇒ Object
625 626 627 |
# File 'lib/minitest/unit.rb', line 625 def @options ||= {} end |
#runner ⇒ Object
Returns the value of attribute runner.
5 6 7 |
# File 'lib/minitest/benchmark.rb', line 5 def runner @runner end |
#start_time ⇒ Object
:nodoc:
620 621 622 |
# File 'lib/minitest/unit.rb', line 620 def start_time @start_time end |
#test_count ⇒ Object
:nodoc:
619 620 621 |
# File 'lib/minitest/unit.rb', line 619 def test_count @test_count end |
#verbose ⇒ Object
:nodoc:
622 623 624 |
# File 'lib/minitest/unit.rb', line 622 def verbose @verbose end |
Class Method Details
.after_tests ⇒ Object
638 639 640 |
# File 'lib/minitest/unit.rb', line 638 def self.after_tests at_exit { at_exit { yield } } end |
.autorun ⇒ Object
Registers MiniTest::Unit to run tests at process exit
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/minitest/unit.rb', line 645 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 |
.out ⇒ Object
Returns the stream to use for output.
DEPRECATED: use ::output instead.
674 675 676 677 |
# File 'lib/minitest/unit.rb', line 674 def self.out warn "::out deprecated, use ::output instead." if $VERBOSE output end |
.output ⇒ Object
Returns the stream to use for output.
665 666 667 |
# File 'lib/minitest/unit.rb', line 665 def self.output @@out end |
.output=(stream) ⇒ Object
Sets MiniTest::Unit to write output to stream
. $stdout is the default output
683 684 685 |
# File 'lib/minitest/unit.rb', line 683 def self.output= stream @@out = stream end |
.plugins ⇒ Object
Return all plugins’ run methods (methods that start with “run_”).
707 708 709 710 711 |
# File 'lib/minitest/unit.rb', line 707 def self.plugins @@plugins ||= (["run_tests"] + public_instance_methods(false). grep(/^run_/).map { |s| s.to_s }).uniq end |
.runner ⇒ Object
Returns the MiniTest::Unit subclass instance that will be used to run the tests. A MiniTest::Unit instance is the default runner.
700 701 702 |
# File 'lib/minitest/unit.rb', line 700 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.
691 692 693 |
# File 'lib/minitest/unit.rb', line 691 def self.runner= runner @@runner = runner end |
Instance Method Details
#_run(args = []) ⇒ Object
Top level driver, controls all output and filtering.
883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'lib/minitest/unit.rb', line 883 def _run args = [] self. = 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
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 755 756 757 758 759 760 |
# File 'lib/minitest/unit.rb', line 725 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
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 |
# File 'lib/minitest/unit.rb', line 766 def _run_suite suite, type header = "#{type}_suite_header" puts send(header, suite) if respond_to? header filter = [: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
762 763 764 |
# File 'lib/minitest/unit.rb', line 762 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:
793 794 795 796 797 798 799 800 |
# File 'lib/minitest/unit.rb', line 793 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 |
#output ⇒ Object
713 714 715 |
# File 'lib/minitest/unit.rb', line 713 def output self.class.output end |
#print(*a) ⇒ Object
:nodoc:
721 722 723 |
# File 'lib/minitest/unit.rb', line 721 def print *a # :nodoc: output.print(*a) end |
#process_args(args = []) ⇒ Object
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
# File 'lib/minitest/unit.rb', line 830 def process_args args = [] = {} orig_args = args.dup OptionParser.new do |opts| opts. = '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| [:seed] = m.to_i end opts.on '-v', '--verbose', "Verbose. Show progress processing files." do [:verbose] = true end opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a| [:filter] = a end opts.parse! args orig_args -= args end unless [:seed] then srand [:seed] = srand % 0xFFFF orig_args << "--seed" << [:seed].to_s end srand [:seed] self.verbose = [:verbose] @help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " " end |
#puke(klass, meth, e) ⇒ Object
Writes status for failed test meth
in klass
which finished with exception e
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/minitest/unit.rb', line 806 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.}\n" when MiniTest::Assertion then @failures += 1 "Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.}\n" else @errors += 1 bt = MiniTest::filter_backtrace(e.backtrace).join "\n " "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.}\n #{bt}\n" end @report << e e[0, 1] end |
#puts(*a) ⇒ Object
:nodoc:
717 718 719 |
# File 'lib/minitest/unit.rb', line 717 def puts *a # :nodoc: output.puts(*a) end |
#run(args = []) ⇒ Object
Begins the full test run. Delegates to runner
‘s #_run method.
876 877 878 |
# File 'lib/minitest/unit.rb', line 876 def run args = [] self.class.runner._run(args) end |
#run_benchmarks ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/minitest/benchmark.rb', line 7 def run_benchmarks # :nodoc: _run_anything :benchmark end |
#run_tests ⇒ Object
Runs test suites matching filter
.
901 902 903 |
# File 'lib/minitest/unit.rb', line 901 def run_tests _run_anything :test end |
#status(io = self.output) ⇒ Object
Writes status to io
908 909 910 911 |
# File 'lib/minitest/unit.rb', line 908 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 |