Class: MiniTest::Unit
- Defined in:
- lib/minitest/benchmark.rb,
lib/minitest/unit.rb
Overview
:nodoc:
Defined Under Namespace
Modules: Deprecated, Guard, LifecycleHooks Classes: TestCase
Constant Summary collapse
- VERSION =
:nodoc:
"4.3.3"
- @@out =
$stdout
- @@after_tests =
[]
Instance Attribute Summary collapse
-
#assertion_count ⇒ Object
:nodoc:.
-
#errors ⇒ Object
:nodoc:.
-
#failures ⇒ Object
:nodoc:.
-
#help ⇒ Object
:nodoc:.
-
#options ⇒ Object
Lazy accessor for options.
-
#report ⇒ Object
:nodoc:.
-
#skips ⇒ Object
:nodoc:.
-
#start_time ⇒ Object
:nodoc:.
-
#test_count ⇒ Object
:nodoc:.
-
#verbose ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.after_tests(&block) ⇒ Object
A simple hook allowing you to run a block of code after all of the tests are done.
-
.autorun ⇒ Object
Registers MiniTest::Unit to run tests at process exit.
-
.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
Runner for a given
type
(eg, test vs bench). -
#_run_suite(suite, type) ⇒ Object
Run a single
suite
for a giventype
. -
#_run_suites(suites, type) ⇒ Object
Runs all the
suites
for a giventype
. -
#benchmark_suite_header(suite) ⇒ Object
:nodoc:.
-
#initialize ⇒ Unit
constructor
:nodoc:.
-
#location(e) ⇒ Object
:nodoc:.
-
#output ⇒ Object
Return the IO for output.
-
#print(*a) ⇒ Object
:nodoc:.
-
#process_args(args = []) ⇒ Object
:nodoc:.
-
#puke(klass, meth, e) ⇒ Object
Writes status for failed test
meth
inklass
which finished with exceptione
. -
#puts(*a) ⇒ Object
:nodoc:.
-
#record(suite, method, assertions, time, error) ⇒ Object
Record the result of a single run.
-
#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
. -
#synchronize ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Unit
:nodoc:
970 971 972 973 974 975 |
# File 'lib/minitest/unit.rb', line 970 def initialize # :nodoc: @report = [] @errors = @failures = @skips = 0 @verbose = false @mutex = Mutex.new end |
Instance Attribute Details
#assertion_count ⇒ Object
:nodoc:
736 737 738 |
# File 'lib/minitest/unit.rb', line 736 def assertion_count @assertion_count end |
#failures ⇒ Object
:nodoc:
735 736 737 |
# File 'lib/minitest/unit.rb', line 735 def failures @failures end |
#options ⇒ Object
Lazy accessor for options.
745 746 747 |
# File 'lib/minitest/unit.rb', line 745 def @options ||= {} end |
#start_time ⇒ Object
:nodoc:
737 738 739 |
# File 'lib/minitest/unit.rb', line 737 def start_time @start_time end |
#test_count ⇒ Object
:nodoc:
736 737 738 |
# File 'lib/minitest/unit.rb', line 736 def test_count @test_count end |
#verbose ⇒ Object
:nodoc:
739 740 741 |
# File 'lib/minitest/unit.rb', line 739 def verbose @verbose end |
Class Method Details
.after_tests(&block) ⇒ Object
759 760 761 |
# File 'lib/minitest/unit.rb', line 759 def self.after_tests &block @@after_tests << block end |
.autorun ⇒ Object
Registers MiniTest::Unit to run tests at process exit
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# File 'lib/minitest/unit.rb', line 766 def self.autorun at_exit { # don't run if there was a non-exit exception next if $! and not $!.kind_of? SystemExit # 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 { @@after_tests.reverse_each(&:call) exit false if exit_code && exit_code != 0 } exit_code = MiniTest::Unit.new.run ARGV } unless @@installed_at_exit @@installed_at_exit = true end |
.output ⇒ Object
Returns the stream to use for output.
790 791 792 |
# File 'lib/minitest/unit.rb', line 790 def self.output @@out end |
.output=(stream) ⇒ Object
Sets MiniTest::Unit to write output to stream
. $stdout is the default output
798 799 800 |
# File 'lib/minitest/unit.rb', line 798 def self.output= stream @@out = stream end |
.plugins ⇒ Object
Return all plugins’ run methods (methods that start with “run_”).
822 823 824 825 826 |
# File 'lib/minitest/unit.rb', line 822 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.
815 816 817 |
# File 'lib/minitest/unit.rb', line 815 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.
806 807 808 |
# File 'lib/minitest/unit.rb', line 806 def self.runner= runner @@runner = runner end |
Instance Method Details
#_run(args = []) ⇒ Object
Top level driver, controls all output and filtering.
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/minitest/unit.rb', line 1034 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
Runner for a given type
(eg, test vs bench).
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 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/minitest/unit.rb', line 846 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
.
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
# File 'lib/minitest/unit.rb', line 898 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 if @verbose result = inst.run self print "%.2f s = " % (Time.now - start_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
. Runs suites declaring a test_order of :parallel
in parallel, and everything else serial.
888 889 890 891 892 893 |
# File 'lib/minitest/unit.rb', line 888 def _run_suites suites, type parallel, serial = suites.partition { |s| s.test_order == :parallel } ParallelEach.new(parallel).map { |suite| _run_suite suite, type } + serial.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:
939 940 941 942 943 944 945 946 |
# File 'lib/minitest/unit.rb', line 939 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
Return the IO for output.
831 832 833 |
# File 'lib/minitest/unit.rb', line 831 def output self.class.output end |
#print(*a) ⇒ Object
:nodoc:
839 840 841 |
# File 'lib/minitest/unit.rb', line 839 def print *a # :nodoc: output.print(*a) end |
#process_args(args = []) ⇒ Object
:nodoc:
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 |
# File 'lib/minitest/unit.rb', line 981 def process_args args = [] # :nodoc: = {} 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 (e.g. /foo/)" 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
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'lib/minitest/unit.rb', line 952 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:
835 836 837 |
# File 'lib/minitest/unit.rb', line 835 def puts *a # :nodoc: output.puts(*a) end |
#record(suite, method, assertions, time, error) ⇒ Object
936 937 |
# File 'lib/minitest/unit.rb', line 936 def record suite, method, assertions, time, error end |
#run(args = []) ⇒ Object
Begins the full test run. Delegates to runner
‘s #_run method.
1027 1028 1029 |
# File 'lib/minitest/unit.rb', line 1027 def run args = [] self.class.runner._run(args) end |
#run_benchmarks ⇒ Object
:nodoc:
5 6 7 |
# File 'lib/minitest/benchmark.rb', line 5 def run_benchmarks # :nodoc: _run_anything :benchmark end |
#run_tests ⇒ Object
Runs test suites matching filter
.
1052 1053 1054 |
# File 'lib/minitest/unit.rb', line 1052 def run_tests _run_anything :test end |
#status(io = self.output) ⇒ Object
Writes status to io
1059 1060 1061 1062 |
# File 'lib/minitest/unit.rb', line 1059 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 |
#synchronize ⇒ Object
:nodoc:
977 978 979 |
# File 'lib/minitest/unit.rb', line 977 def synchronize # :nodoc: @mutex.synchronize { yield } end |