How to use msg method of Assertions Package

Best Minitest_ruby code snippet using Assertions.msg

unit.rb

Source:unit.rb Github

copy

Full Screen

...7module MiniTest8 def self.const_missing name # :nodoc:9 case name10 when :MINI_DIR then11 msg = "MiniTest::MINI_DIR was removed. Don't violate other's internals."12 warn "WAR\NING: #{msg}"13 warn "WAR\NING: Used by #{caller.first}."14 const_set :MINI_DIR, "bad value"15 else16 super17 end18 end19 ##20 # Assertion base class21 class Assertion < Exception; end22 ##23 # Assertion raised when skipping a test24 class Skip < Assertion; end25 class << self26 ##27 # Filter object for backtraces.28 attr_accessor :backtrace_filter29 end30 class BacktraceFilter # :nodoc:31 def filter bt32 return ["No backtrace"] unless bt33 new_bt = []34 unless $DEBUG then35 bt.each do |line|36 break if line =~ /lib\/minitest/37 new_bt << line38 end39 new_bt = bt.reject { |line| line =~ /lib\/minitest/ } if new_bt.empty?40 new_bt = bt.dup if new_bt.empty?41 else42 new_bt = bt.dup43 end44 new_bt45 end46 end47 self.backtrace_filter = BacktraceFilter.new48 def self.filter_backtrace bt # :nodoc:49 backtrace_filter.filter bt50 end51 ##52 # MiniTest Assertions. All assertion methods accept a +msg+ which is53 # printed if the assertion fails.54 module Assertions55 UNDEFINED = Object.new # :nodoc:56 def UNDEFINED.inspect # :nodoc:57 "UNDEFINED" # again with the rdoc bugs... :(58 end59 ##60 # Returns the diff command to use in #diff. Tries to intelligently61 # figure out what diff to use.62 def self.diff63 @diff = if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ &&64 system("diff.exe", __FILE__, __FILE__)) then65 "diff.exe -u"66 elsif Minitest::Unit::Guard.maglev? then # HACK67 "diff -u"68 elsif system("gdiff", __FILE__, __FILE__)69 "gdiff -u" # solaris and kin suck70 elsif system("diff", __FILE__, __FILE__)71 "diff -u"72 else73 nil74 end unless defined? @diff75 @diff76 end77 ##78 # Set the diff command to use in #diff.79 def self.diff= o80 @diff = o81 end82 ##83 # Returns a diff between +exp+ and +act+. If there is no known84 # diff command or if it doesn't make sense to diff the output85 # (single line, short output), then it simply returns a basic86 # comparison between the two.87 def diff exp, act88 require "tempfile"89 expect = mu_pp_for_diff exp90 butwas = mu_pp_for_diff act91 result = nil92 need_to_diff =93 MiniTest::Assertions.diff &&94 (expect.include?("\n") ||95 butwas.include?("\n") ||96 expect.size > 30 ||97 butwas.size > 30 ||98 expect == butwas)99 return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless100 need_to_diff101 Tempfile.open("expect") do |a|102 a.puts expect103 a.flush104 Tempfile.open("butwas") do |b|105 b.puts butwas106 b.flush107 result = `#{MiniTest::Assertions.diff} #{a.path} #{b.path}`108 result.sub!(/^\-\-\- .+/, "--- expected")109 result.sub!(/^\+\+\+ .+/, "+++ actual")110 if result.empty? then111 klass = exp.class112 result = [113 "No visible difference in the #{klass}#inspect output.\n",114 "You should look at the implementation of #== on ",115 "#{klass} or its members.\n",116 expect,117 ].join118 end119 end120 end121 result122 end123 ##124 # This returns a human-readable version of +obj+. By default125 # #inspect is called. You can override this to use #pretty_print126 # if you want.127 def mu_pp obj128 s = obj.inspect129 s = s.encode Encoding.default_external if defined? Encoding130 s131 end132 ##133 # This returns a diff-able human-readable version of +obj+. This134 # differs from the regular mu_pp because it expands escaped135 # newlines and makes hex-values generic (like object_ids). This136 # uses mu_pp to do the first pass and then cleans it up.137 def mu_pp_for_diff obj138 mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ':0xXXXXXX')139 end140 def _assertions= n # :nodoc:141 @_assertions = n142 end143 def _assertions # :nodoc:144 @_assertions ||= 0145 end146 ##147 # Fails unless +test+ is a true value.148 def assert test, msg = nil149 msg ||= "Failed assertion, no message given."150 self._assertions += 1151 unless test then152 msg = msg.call if Proc === msg153 raise MiniTest::Assertion, msg154 end155 true156 end157 ##158 # Fails unless +obj+ is empty.159 def assert_empty obj, msg = nil160 msg = message(msg) { "Expected #{mu_pp(obj)} to be empty" }161 assert_respond_to obj, :empty?162 assert obj.empty?, msg163 end164 ##165 # Fails unless <tt>exp == act</tt> printing the difference between166 # the two, if possible.167 #168 # If there is no visible difference but the assertion fails, you169 # should suspect that your #== is buggy, or your inspect output is170 # missing crucial details.171 #172 # For floats use assert_in_delta.173 #174 # See also: MiniTest::Assertions.diff175 def assert_equal exp, act, msg = nil176 msg = message(msg, "") { diff exp, act }177 assert exp == act, msg178 end179 ##180 # For comparing Floats. Fails unless +exp+ and +act+ are within +delta+181 # of each other.182 #183 # assert_in_delta Math::PI, (22.0 / 7.0), 0.01184 def assert_in_delta exp, act, delta = 0.001, msg = nil185 n = (exp - act).abs186 msg = message(msg) {187 "Expected |#{exp} - #{act}| (#{n}) to be <= #{delta}"188 }189 assert delta >= n, msg190 end191 ##192 # For comparing Floats. Fails unless +exp+ and +act+ have a relative193 # error less than +epsilon+.194 def assert_in_epsilon a, b, epsilon = 0.001, msg = nil195 assert_in_delta a, b, [a.abs, b.abs].min * epsilon, msg196 end197 ##198 # Fails unless +collection+ includes +obj+.199 def assert_includes collection, obj, msg = nil200 msg = message(msg) {201 "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}"202 }203 assert_respond_to collection, :include?204 assert collection.include?(obj), msg205 end206 ##207 # Fails unless +obj+ is an instance of +cls+.208 def assert_instance_of cls, obj, msg = nil209 msg = message(msg) {210 "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}"211 }212 assert obj.instance_of?(cls), msg213 end214 ##215 # Fails unless +obj+ is a kind of +cls+.216 def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of217 msg = message(msg) {218 "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" }219 assert obj.kind_of?(cls), msg220 end221 ##222 # Fails unless +matcher+ <tt>=~</tt> +obj+.223 def assert_match matcher, obj, msg = nil224 msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }225 assert_respond_to matcher, :"=~"226 matcher = Regexp.new Regexp.escape matcher if String === matcher227 assert matcher =~ obj, msg228 end229 ##230 # Fails unless +obj+ is nil231 def assert_nil obj, msg = nil232 msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" }233 assert obj.nil?, msg234 end235 ##236 # For testing with binary operators.237 #238 # assert_operator 5, :<=, 4239 def assert_operator o1, op, o2 = UNDEFINED, msg = nil240 return assert_predicate o1, op, msg if UNDEFINED == o2241 msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }242 assert o1.__send__(op, o2), msg243 end244 ##245 # Fails if stdout or stderr do not output the expected results.246 # Pass in nil if you don't care about that streams output. Pass in247 # "" if you require it to be silent. Pass in a regexp if you want248 # to pattern match.249 #250 # NOTE: this uses #capture_io, not #capture_subprocess_io.251 #252 # See also: #assert_silent253 def assert_output stdout = nil, stderr = nil254 out, err = capture_io do255 yield256 end257 err_msg = Regexp === stderr ? :assert_match : :assert_equal if stderr258 out_msg = Regexp === stdout ? :assert_match : :assert_equal if stdout259 y = send err_msg, stderr, err, "In stderr" if err_msg260 x = send out_msg, stdout, out, "In stdout" if out_msg261 (!stdout || x) && (!stderr || y)262 end263 ##264 # For testing with predicates.265 #266 # assert_predicate str, :empty?267 #268 # This is really meant for specs and is front-ended by assert_operator:269 #270 # str.must_be :empty?271 def assert_predicate o1, op, msg = nil272 msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op}" }273 assert o1.__send__(op), msg274 end275 ##276 # Fails unless the block raises one of +exp+. Returns the277 # exception matched so you can check the message, attributes, etc.278 def assert_raises *exp279 msg = "#{exp.pop}.\n" if String === exp.last280 begin281 yield282 rescue MiniTest::Skip => e283 return e if exp.include? MiniTest::Skip284 raise e285 rescue Exception => e286 expected = exp.any? { |ex|287 if ex.instance_of? Module then288 e.kind_of? ex289 else290 e.instance_of? ex291 end292 }293 assert expected, proc {294 exception_details(e, "#{msg}#{mu_pp(exp)} exception expected, not")295 }296 return e297 end298 exp = exp.first if exp.size == 1299 flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised."300 end301 ##302 # Fails unless +obj+ responds to +meth+.303 def assert_respond_to obj, meth, msg = nil304 msg = message(msg) {305 "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}"306 }307 assert obj.respond_to?(meth), msg308 end309 ##310 # Fails unless +exp+ and +act+ are #equal?311 def assert_same exp, act, msg = nil312 msg = message(msg) {313 data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]314 "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data315 }316 assert exp.equal?(act), msg317 end318 ##319 # +send_ary+ is a receiver, message and arguments.320 #321 # Fails unless the call returns a true value322 # TODO: I should prolly remove this from specs323 def assert_send send_ary, m = nil324 recv, msg, *args = send_ary325 m = message(m) {326 "Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" }327 assert recv.__send__(msg, *args), m328 end329 ##330 # Fails if the block outputs anything to stderr or stdout.331 #332 # See also: #assert_output333 def assert_silent334 assert_output "", "" do335 yield336 end337 end338 ##339 # Fails unless the block throws +sym+340 def assert_throws sym, msg = nil341 default = "Expected #{mu_pp(sym)} to have been thrown"342 caught = true343 catch(sym) do344 begin345 yield346 rescue ThreadError => e # wtf?!? 1.8 + threads == suck347 default += ", not \:#{e.message[/uncaught throw \`(\w+?)\'/, 1]}"348 rescue ArgumentError => e # 1.9 exception349 default += ", not #{e.message.split(/ /).last}"350 rescue NameError => e # 1.8 exception351 default += ", not #{e.name.inspect}"352 end353 caught = false354 end355 assert caught, message(msg) { default }356 end357 ##358 # Captures $stdout and $stderr into strings:359 #360 # out, err = capture_io do361 # puts "Some info"362 # warn "You did a bad thing"363 # end364 #365 # assert_match %r%info%, out366 # assert_match %r%bad%, err367 #368 # NOTE: For efficiency, this method uses StringIO and does not369 # capture IO for subprocesses. Use #capture_subprocess_io for370 # that.371 def capture_io372 require 'stringio'373 captured_stdout, captured_stderr = StringIO.new, StringIO.new374 synchronize do375 orig_stdout, orig_stderr = $stdout, $stderr376 $stdout, $stderr = captured_stdout, captured_stderr377 begin378 yield379 ensure380 $stdout = orig_stdout381 $stderr = orig_stderr382 end383 end384 return captured_stdout.string, captured_stderr.string385 end386 ##387 # Captures $stdout and $stderr into strings, using Tempfile to388 # ensure that subprocess IO is captured as well.389 #390 # out, err = capture_subprocess_io do391 # system "echo Some info"392 # system "echo You did a bad thing 1>&2"393 # end394 #395 # assert_match %r%info%, out396 # assert_match %r%bad%, err397 #398 # NOTE: This method is approximately 10x slower than #capture_io so399 # only use it when you need to test the output of a subprocess.400 def capture_subprocess_io401 require 'tempfile'402 captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")403 synchronize do404 orig_stdout, orig_stderr = $stdout.dup, $stderr.dup405 $stdout.reopen captured_stdout406 $stderr.reopen captured_stderr407 begin408 yield409 $stdout.rewind410 $stderr.rewind411 [captured_stdout.read, captured_stderr.read]412 ensure413 captured_stdout.unlink414 captured_stderr.unlink415 $stdout.reopen orig_stdout416 $stderr.reopen orig_stderr417 end418 end419 end420 ##421 # Returns details for exception +e+422 def exception_details e, msg423 [424 "#{msg}",425 "Class: <#{e.class}>",426 "Message: <#{e.message.inspect}>",427 "---Backtrace---",428 "#{MiniTest::filter_backtrace(e.backtrace).join("\n")}",429 "---------------",430 ].join "\n"431 end432 ##433 # Fails with +msg+434 def flunk msg = nil435 msg ||= "Epic Fail!"436 assert false, msg437 end438 ##439 # Returns a proc that will output +msg+ along with the default message.440 def message msg = nil, ending = ".", &default441 proc {442 msg = msg.call.chomp(".") if Proc === msg443 custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?444 "#{custom_message}#{default.call}#{ending}"445 }446 end447 ##448 # used for counting assertions449 def pass msg = nil450 assert true451 end452 ##453 # Fails if +test+ is a true value454 def refute test, msg = nil455 msg ||= "Failed refutation, no message given"456 not assert(! test, msg)457 end458 ##459 # Fails if +obj+ is empty.460 def refute_empty obj, msg = nil461 msg = message(msg) { "Expected #{mu_pp(obj)} to not be empty" }462 assert_respond_to obj, :empty?463 refute obj.empty?, msg464 end465 ##466 # Fails if <tt>exp == act</tt>.467 #468 # For floats use refute_in_delta.469 def refute_equal exp, act, msg = nil470 msg = message(msg) {471 "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}"472 }473 refute exp == act, msg474 end475 ##476 # For comparing Floats. Fails if +exp+ is within +delta+ of +act+.477 #478 # refute_in_delta Math::PI, (22.0 / 7.0)479 def refute_in_delta exp, act, delta = 0.001, msg = nil480 n = (exp - act).abs481 msg = message(msg) {482 "Expected |#{exp} - #{act}| (#{n}) to not be <= #{delta}"483 }484 refute delta >= n, msg485 end486 ##487 # For comparing Floats. Fails if +exp+ and +act+ have a relative error488 # less than +epsilon+.489 def refute_in_epsilon a, b, epsilon = 0.001, msg = nil490 refute_in_delta a, b, a * epsilon, msg491 end492 ##493 # Fails if +collection+ includes +obj+.494 def refute_includes collection, obj, msg = nil495 msg = message(msg) {496 "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}"497 }498 assert_respond_to collection, :include?499 refute collection.include?(obj), msg500 end501 ##502 # Fails if +obj+ is an instance of +cls+.503 def refute_instance_of cls, obj, msg = nil504 msg = message(msg) {505 "Expected #{mu_pp(obj)} to not be an instance of #{cls}"506 }507 refute obj.instance_of?(cls), msg508 end509 ##510 # Fails if +obj+ is a kind of +cls+.511 def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of512 msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" }513 refute obj.kind_of?(cls), msg514 end515 ##516 # Fails if +matcher+ <tt>=~</tt> +obj+.517 def refute_match matcher, obj, msg = nil518 msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"}519 assert_respond_to matcher, :"=~"520 matcher = Regexp.new Regexp.escape matcher if String === matcher521 refute matcher =~ obj, msg522 end523 ##524 # Fails if +obj+ is nil.525 def refute_nil obj, msg = nil526 msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" }527 refute obj.nil?, msg528 end529 ##530 # Fails if +o1+ is not +op+ +o2+. Eg:531 #532 # refute_operator 1, :>, 2 #=> pass533 # refute_operator 1, :<, 2 #=> fail534 def refute_operator o1, op, o2 = UNDEFINED, msg = nil535 return refute_predicate o1, op, msg if UNDEFINED == o2536 msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"}537 refute o1.__send__(op, o2), msg538 end539 ##540 # For testing with predicates.541 #542 # refute_predicate str, :empty?543 #544 # This is really meant for specs and is front-ended by refute_operator:545 #546 # str.wont_be :empty?547 def refute_predicate o1, op, msg = nil548 msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op}" }549 refute o1.__send__(op), msg550 end551 ##552 # Fails if +obj+ responds to the message +meth+.553 def refute_respond_to obj, meth, msg = nil554 msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" }555 refute obj.respond_to?(meth), msg556 end557 ##558 # Fails if +exp+ is the same (by object identity) as +act+.559 def refute_same exp, act, msg = nil560 msg = message(msg) {561 data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]562 "Expected %s (oid=%d) to not be the same as %s (oid=%d)" % data563 }564 refute exp.equal?(act), msg565 end566 ##567 # Skips the current test. Gets listed at the end of the run but568 # doesn't cause a failure exit code.569 def skip msg = nil, bt = caller570 msg ||= "Skipped, no message given"571 @skip = true572 raise MiniTest::Skip, msg, bt573 end574 ##575 # Was this testcase skipped? Meant for #teardown.576 def skipped?577 defined?(@skip) and @skip578 end579 ##580 # Takes a block and wraps it with the runner's shared mutex.581 def synchronize582 Minitest::Unit.runner.synchronize do583 yield584 end585 end586 end587 class Unit # :nodoc:588 VERSION = "4.7.5" # :nodoc:589 attr_accessor :report, :failures, :errors, :skips # :nodoc:590 attr_accessor :assertion_count # :nodoc:591 attr_writer :test_count # :nodoc:592 attr_accessor :start_time # :nodoc:593 attr_accessor :help # :nodoc:594 attr_accessor :verbose # :nodoc:595 attr_writer :options # :nodoc:596 ##597 # :attr:598 #599 # if true, installs an "INFO" signal handler (only available to BSD and600 # OS X users) which prints diagnostic information about the test run.601 #602 # This is auto-detected by default but may be overridden by custom603 # runners.604 attr_accessor :info_signal605 ##606 # Lazy accessor for options.607 def options608 @options ||= {}609 end610 @@installed_at_exit ||= false611 @@out = $stdout612 @@after_tests = []613 ##614 # A simple hook allowing you to run a block of code after _all_ of615 # the tests are done. Eg:616 #617 # MiniTest::Unit.after_tests { p $debugging_info }618 def self.after_tests &block619 @@after_tests << block620 end621 ##622 # Registers MiniTest::Unit to run tests at process exit623 def self.autorun624 at_exit {625 # don't run if there was a non-exit exception626 next if $! and not $!.kind_of? SystemExit627 # the order here is important. The at_exit handler must be628 # installed before anyone else gets a chance to install their629 # own, that way we can be assured that our exit will be last630 # to run (at_exit stacks).631 exit_code = nil632 at_exit {633 @@after_tests.reverse_each(&:call)634 exit false if exit_code && exit_code != 0635 }636 exit_code = MiniTest::Unit.new.run ARGV637 } unless @@installed_at_exit638 @@installed_at_exit = true639 end640 ##641 # Returns the stream to use for output.642 def self.output643 @@out644 end645 ##646 # Sets MiniTest::Unit to write output to +stream+. $stdout is the default647 # output648 def self.output= stream649 @@out = stream650 end651 ##652 # Tells MiniTest::Unit to delegate to +runner+, an instance of a653 # MiniTest::Unit subclass, when MiniTest::Unit#run is called.654 def self.runner= runner655 @@runner = runner656 end657 ##658 # Returns the MiniTest::Unit subclass instance that will be used659 # to run the tests. A MiniTest::Unit instance is the default660 # runner.661 def self.runner662 @@runner ||= self.new663 end664 ##665 # Return all plugins' run methods (methods that start with "run_").666 def self.plugins667 @@plugins ||= (["run_tests"] +668 public_instance_methods(false).669 grep(/^run_/).map { |s| s.to_s }).uniq670 end671 ##672 # Return the IO for output.673 def output674 self.class.output675 end676 def puts *a # :nodoc:677 output.puts(*a)678 end679 def print *a # :nodoc:680 output.print(*a)681 end682 def test_count # :nodoc:683 @test_count ||= 0684 end685 ##686 # Runner for a given +type+ (eg, test vs bench).687 def _run_anything type688 suites = TestCase.send "#{type}_suites"689 return if suites.empty?690 start = Time.now691 puts692 puts "# Running #{type}s:"693 puts694 @test_count, @assertion_count = 0, 0695 sync = output.respond_to? :"sync=" # stupid emacs696 old_sync, output.sync = output.sync, true if sync697 results = _run_suites suites, type698 @test_count = results.inject(0) { |sum, (tc, _)| sum + tc }699 @assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac }700 output.sync = old_sync if sync701 t = Time.now - start702 puts703 puts704 puts "Finished #{type}s in %.6fs, %.4f tests/s, %.4f assertions/s." %705 [t, test_count / t, assertion_count / t]706 report.each_with_index do |msg, i|707 puts "\n%3d) %s" % [i + 1, msg]708 end709 puts710 status711 end712 ##713 # Runs all the +suites+ for a given +type+.714 #715 # NOTE: this method is redefined in parallel_each.rb, which is716 # loaded if a test-suite calls parallelize_me!.717 def _run_suites suites, type718 suites.map { |suite| _run_suite suite, type }719 end720 ##721 # Run a single +suite+ for a given +type+.722 def _run_suite suite, type723 header = "#{type}_suite_header"724 puts send(header, suite) if respond_to? header725 filter = options[:filter] || '/./'726 filter = Regexp.new $1 if filter =~ /\/(.*)\//727 all_test_methods = suite.send "#{type}_methods"728 filtered_test_methods = all_test_methods.find_all { |m|729 filter === m || filter === "#{suite}##{m}"730 }731 assertions = filtered_test_methods.map { |method|732 inst = suite.new method733 inst._assertions = 0734 print "#{suite}##{method} = " if @verbose735 start_time = Time.now if @verbose736 result = inst.run self737 print "%.2f s = " % (Time.now - start_time) if @verbose738 print result739 puts if @verbose740 inst._assertions741 }742 return assertions.size, assertions.inject(0) { |sum, n| sum + n }743 end744 ##745 # Record the result of a single test. Makes it very easy to gather746 # information. Eg:747 #748 # class StatisticsRecorder < MiniTest::Unit749 # def record suite, method, assertions, time, error750 # # ... record the results somewhere ...751 # end752 # end753 #754 # MiniTest::Unit.runner = StatisticsRecorder.new755 #756 # NOTE: record might be sent more than once per test. It will be757 # sent once with the results from the test itself. If there is a758 # failure or error in teardown, it will be sent again with the759 # error or failure.760 def record suite, method, assertions, time, error761 end762 def location e # :nodoc:763 last_before_assertion = ""764 e.backtrace.reverse_each do |s|765 break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/766 last_before_assertion = s767 end768 last_before_assertion.sub(/:in .*$/, '')769 end770 ##771 # Writes status for failed test +meth+ in +klass+ which finished with772 # exception +e+773 def puke klass, meth, e774 e = case e775 when MiniTest::Skip then776 @skips += 1777 return "S" unless @verbose778 "Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"779 when MiniTest::Assertion then780 @failures += 1781 "Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"782 else783 @errors += 1784 bt = MiniTest::filter_backtrace(e.backtrace).join "\n "785 "Error:\n#{klass}##{meth}:\n#{e.class}: #{e.message}\n #{bt}\n"786 end787 @report << e788 e[0, 1]789 end790 def initialize # :nodoc:791 @report = []792 @errors = @failures = @skips = 0793 @verbose = false794 @mutex = defined?(Mutex) ? Mutex.new : nil795 @info_signal = Signal.list['INFO']796 end797 def synchronize # :nodoc:798 if @mutex then799 @mutex.synchronize { yield }800 else801 yield802 end803 end804 def process_args args = [] # :nodoc:805 options = {}806 orig_args = args.dup807 OptionParser.new do |opts|808 opts.banner = 'minitest options:'809 opts.version = MiniTest::Unit::VERSION810 opts.on '-h', '--help', 'Display this help.' do811 puts opts812 exit813 end814 opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|815 options[:seed] = m.to_i816 end817 opts.on '-v', '--verbose', "Verbose. Show progress processing files." do818 options[:verbose] = true819 end820 opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a|821 options[:filter] = a822 end823 opts.parse! args824 orig_args -= args825 end826 unless options[:seed] then827 srand828 options[:seed] = srand % 0xFFFF829 orig_args << "--seed" << options[:seed].to_s830 end831 srand options[:seed]832 self.verbose = options[:verbose]833 @help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " "834 options835 end836 ##837 # Begins the full test run. Delegates to +runner+'s #_run method.838 def run args = []839 self.class.runner._run(args)840 end841 ##842 # Top level driver, controls all output and filtering.843 def _run args = []844 args = process_args args # ARGH!! blame test/unit process_args845 self.options.merge! args846 puts "Run options: #{help}"847 self.class.plugins.each do |plugin|848 send plugin849 break unless report.empty?850 end851 return failures + errors if self.test_count > 0 # or return nil...852 rescue Interrupt853 abort 'Interrupted'854 end855 ##856 # Runs test suites matching +filter+.857 def run_tests858 _run_anything :test859 end860 ##861 # Writes status to +io+862 def status io = self.output863 format = "%d tests, %d assertions, %d failures, %d errors, %d skips"864 io.puts format % [test_count, assertion_count, failures, errors, skips]865 end866 ##867 # Provides a simple set of guards that you can use in your tests868 # to skip execution if it is not applicable. These methods are869 # mixed into TestCase as both instance and class methods so you870 # can use them inside or outside of the test methods.871 #872 # def test_something_for_mri873 # skip "bug 1234" if jruby?874 # # ...875 # end876 #877 # if windows? then878 # # ... lots of test methods ...879 # end880 module Guard881 ##882 # Is this running on jruby?883 def jruby? platform = RUBY_PLATFORM884 "java" == platform885 end886 ##887 # Is this running on mri?888 def maglev? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE889 "maglev" == platform890 end891 module_function :maglev?892 ##893 # Is this running on mri?894 def mri? platform = RUBY_DESCRIPTION895 /^ruby/ =~ platform896 end897 ##898 # Is this running on rubinius?899 def rubinius? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE900 "rbx" == platform901 end902 ##903 # Is this running on windows?904 def windows? platform = RUBY_PLATFORM905 /mswin|mingw/ =~ platform906 end907 end908 ##909 # Provides before/after hooks for setup and teardown. These are910 # meant for library writers, NOT for regular test authors. See911 # #before_setup for an example.912 module LifecycleHooks913 ##914 # Runs before every test, after setup. This hook is meant for915 # libraries to extend minitest. It is not meant to be used by916 # test developers.917 #918 # See #before_setup for an example.919 def after_setup; end920 ##921 # Runs before every test, before setup. This hook is meant for922 # libraries to extend minitest. It is not meant to be used by923 # test developers.924 #925 # As a simplistic example:926 #927 # module MyMinitestPlugin928 # def before_setup929 # super930 # # ... stuff to do before setup is run931 # end932 #933 # def after_setup934 # # ... stuff to do after setup is run935 # super936 # end937 #938 # def before_teardown939 # super940 # # ... stuff to do before teardown is run941 # end942 #943 # def after_teardown944 # # ... stuff to do after teardown is run945 # super946 # end947 # end948 #949 # class MiniTest::Unit::TestCase950 # include MyMinitestPlugin951 # end952 def before_setup; end953 ##954 # Runs after every test, before teardown. This hook is meant for955 # libraries to extend minitest. It is not meant to be used by956 # test developers.957 #958 # See #before_setup for an example.959 def before_teardown; end960 ##961 # Runs after every test, after teardown. This hook is meant for962 # libraries to extend minitest. It is not meant to be used by963 # test developers.964 #965 # See #before_setup for an example.966 def after_teardown; end967 end968 ##969 # Subclass TestCase to create your own tests. Typically you'll want a970 # TestCase subclass per implementation class.971 #972 # See MiniTest::Assertions973 class TestCase974 include LifecycleHooks975 include Guard976 extend Guard977 attr_reader :__name__ # :nodoc:978 PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException,979 Interrupt, SystemExit] # :nodoc:980 ##981 # Runs the tests reporting the status to +runner+982 def run runner983 trap "INFO" do984 runner.report.each_with_index do |msg, i|985 warn "\n%3d) %s" % [i + 1, msg]986 end987 warn ''988 time = runner.start_time ? Time.now - runner.start_time : 0989 warn "Current Test: %s#%s %.2fs" % [self.class, self.__name__, time]990 runner.status $stderr991 end if runner.info_signal992 start_time = Time.now993 result = ""994 begin995 @passed = nil996 self.before_setup997 self.setup998 self.after_setup999 self.run_test self.__name__...

Full Screen

Full Screen

test_minitest_reporter.rb

Source:test_minitest_reporter.rb Github

copy

Full Screen

1require "minitest/autorun"2require "minitest/metametameta"3class TestMinitestReporter < Minitest::Test4 attr_accessor :r, :io5 def new_composite_reporter6 reporter = Minitest::CompositeReporter.new7 reporter << Minitest::SummaryReporter.new(self.io)8 reporter << Minitest::ProgressReporter.new(self.io)9 def reporter.first10 reporters.first11 end12 def reporter.results13 first.results14 end15 def reporter.count16 first.count17 end18 def reporter.assertions19 first.assertions20 end21 reporter22 end23 def setup24 self.io = StringIO.new("")25 self.r = new_composite_reporter26 end27 def error_test28 unless defined? @et then29 @et = Minitest::Test.new(:woot)30 @et.failures << Minitest::UnexpectedError.new(begin31 raise "no"32 rescue => e33 e34 end)35 end36 @et37 end38 def fail_test39 unless defined? @ft then40 @ft = Minitest::Test.new(:woot)41 @ft.failures << begin42 raise Minitest::Assertion, "boo"43 rescue Minitest::Assertion => e44 e45 end46 end47 @ft48 end49 def passing_test50 @pt ||= Minitest::Test.new(:woot)51 end52 def skip_test53 unless defined? @st then54 @st = Minitest::Test.new(:woot)55 @st.failures << begin56 raise Minitest::Skip57 rescue Minitest::Assertion => e58 e59 end60 end61 @st62 end63 def test_passed_eh_empty64 assert r.passed?65 end66 def test_passed_eh_failure67 r.results << fail_test68 refute r.passed?69 end70 SKIP_MSG = "\n\nYou have skipped tests. Run with --verbose for details."71 def test_passed_eh_error72 r.start73 r.results << error_test74 refute r.passed?75 r.report76 refute_match SKIP_MSG, io.string77 end78 def test_passed_eh_skipped79 r.start80 r.results << skip_test81 assert r.passed?82 restore_env do83 r.report84 end85 assert_match SKIP_MSG, io.string86 end87 def test_passed_eh_skipped_verbose88 r.first.options[:verbose] = true89 r.start90 r.results << skip_test91 assert r.passed?92 r.report93 refute_match SKIP_MSG, io.string94 end95 def test_start96 r.start97 exp = "Run options: \n\n# Running:\n\n"98 assert_equal exp, io.string99 end100 def test_record_pass101 r.record passing_test102 assert_equal ".", io.string103 assert_empty r.results104 assert_equal 1, r.count105 assert_equal 0, r.assertions106 end107 def test_record_fail108 r.record fail_test109 assert_equal "F", io.string110 assert_equal [fail_test], r.results111 assert_equal 1, r.count112 assert_equal 0, r.assertions113 end114 def test_record_error115 r.record error_test116 assert_equal "E", io.string117 assert_equal [error_test], r.results118 assert_equal 1, r.count119 assert_equal 0, r.assertions120 end121 def test_record_skip122 r.record skip_test123 assert_equal "S", io.string124 assert_equal [skip_test], r.results125 assert_equal 1, r.count126 assert_equal 0, r.assertions127 end128 def normalize_output output129 output.sub!(/Finished in .*/, "Finished in 0.00")130 output.sub!(/Loaded suite .*/, 'Loaded suite blah')131 output.gsub!(/ = \d+.\d\d s = /, ' = 0.00 s = ')132 output.gsub!(/0x[A-Fa-f0-9]+/, '0xXXX')133 output.gsub!(/ +$/, '')134 if windows? then135 output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, '[FILE:LINE]')136 output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')137 else138 output.gsub!(/\[[^\]:]+:\d+\]/, '[FILE:LINE]')139 output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')140 end141 output142 end143 def test_report_empty144 r.start145 r.report146 exp = clean <<-EOM147 Run options:148 # Running:149 Finished in 0.00150 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips151 EOM152 assert_equal exp, normalize_output(io.string)153 end154 def test_report_passing155 r.start156 r.record passing_test157 r.report158 exp = clean <<-EOM159 Run options:160 # Running:161 .162 Finished in 0.00163 1 runs, 0 assertions, 0 failures, 0 errors, 0 skips164 EOM165 assert_equal exp, normalize_output(io.string)166 end167 def test_report_failure168 r.start169 r.record fail_test170 r.report171 exp = clean <<-EOM172 Run options:173 # Running:174 F175 Finished in 0.00176 1) Failure:177 Minitest::Test#woot [FILE:LINE]:178 boo179 1 runs, 0 assertions, 1 failures, 0 errors, 0 skips180 EOM181 assert_equal exp, normalize_output(io.string)182 end183 def test_report_error184 r.start185 r.record error_test186 r.report187 exp = clean <<-EOM188 Run options:189 # Running:190 E191 Finished in 0.00192 1) Error:193 Minitest::Test#woot:194 RuntimeError: no195 FILE:LINE:in `error_test'196 FILE:LINE:in `test_report_error'197 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips198 EOM199 assert_equal exp, normalize_output(io.string)200 end201 def restore_env202 old_value = ENV["MT_NO_SKIP_MSG"]203 ENV.delete "MT_NO_SKIP_MSG"204 205 yield206 ensure207 ENV["MT_NO_SKIP_MSG"] = old_value208 end209 def test_report_skipped210 r.start211 r.record skip_test212 restore_env do213 r.report214 end215 exp = clean <<-EOM216 Run options:217 # Running:218 S219 Finished in 0.00220 1 runs, 0 assertions, 0 failures, 0 errors, 1 skips221 You have skipped tests. Run with --verbose for details.222 EOM223 assert_equal exp, normalize_output(io.string)224 end225end...

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1assert(true, "This should be true")2assert_block("This should be true") { true }3assert_equal(1, 1, "This should be true")4assert_not_equal(1, 2, "This should be true")5assert_in_delta(1, 1.1, 0.2, "This should be true")6assert_in_epsilon(1, 1.1, 0.2, "This should be true")7assert_includes([1,2,3], 2, "This should be true")8assert_instance_of(String, "Hello, World!", "This should be true")

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1 assert_equal(4, msg.length, "Length of message is not 4")2 assert_equal(4, msg.length, "Length of message is not 4")3 assert_equal(4, msg.length, "Length of message is not 4")4 assert_equal(4, msg.length, "Length of message is not 4")5 assert_equal(4, msg.length, "Length of message is not 4")6 assert_equal(4, msg.length, "Length of message is not 4")7 assert_equal(4, msg.length, "Length of message is not 4")8 assert_equal(4, msg.length, "Length of message is not 4")

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1Assertions.msg("Hello World")2Assertions::msg("Hello World")3Assertions.new.msg("Hello World")4Assertions::msg("Hello World")

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1assert(true, "This should be true")2assert_block("This should be true") { true }3assert_equal(1, 1, "This should be true")4assert_not_equal(1, 2, "This should be true")5assert_in_delta(1, 1.1, 0.2, "This should be true")6assert_in_epsilon(1, 1.1, 0.2, "This should be true")7assert_includes([1,2,3], 2, "This should be true")8assert_instance_of(String, "Hello, World!", "This should be true")

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1 assert_equal(4, msg.length, "Length of message is not 4")2 assert_equal(4, msg.length, "Length of message is not 4")3 assert_equal(4, msg.length, "Length of message is not 4")4 assert_equal(4, msg.length, "Length of message is not 4")5 assert_equal(4, msg.length, "Length of message is not 4")6 assert_equal(4, msg.length, "Length of message is not 4")7 assert_equal(4, msg.length, "Length of message is not 4")8 assert_equal(4, msg.length, "Length of message is not 4")

Full Screen

Full Screen

msg

Using AI Code Generation

copy

Full Screen

1Assertions.msg("Hello World")2Assertions::msg("Hello World")3Assertions.new.msg("Hello World")4Assertions::msg("Hello World")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Minitest_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful