Best Minitest_ruby code snippet using Reportable.bt
minitest.rb
Source:minitest.rb
...176 s =~ /[\s|&<>$()]/ ? s.inspect : s177 }.join " "178 options179 end180 def self.filter_backtrace bt # :nodoc:181 backtrace_filter.filter bt182 end183 ##184 # Represents anything "runnable", like Test, Spec, Benchmark, or185 # whatever you can dream up.186 #187 # Subclasses of this are automatically registered and available in188 # Runnable.runnables.189 class Runnable190 ##191 # Number of assertions executed in this run.192 attr_accessor :assertions193 ##194 # An assertion raised during the run, if any.195 attr_accessor :failures196 ##197 # The time it took to run.198 attr_accessor :time199 def time_it # :nodoc:200 t0 = Minitest.clock_time201 yield202 ensure203 self.time = Minitest.clock_time - t0204 end205 ##206 # Name of the run.207 def name208 @NAME209 end210 ##211 # Set the name of the run.212 def name= o213 @NAME = o214 end215 ##216 # Returns all instance methods matching the pattern +re+.217 def self.methods_matching re218 public_instance_methods(true).grep(re).map(&:to_s)219 end220 def self.reset # :nodoc:221 @@runnables = []222 end223 reset224 ##225 # Responsible for running all runnable methods in a given class,226 # each in its own instance. Each instance is passed to the227 # reporter to record.228 def self.run reporter, options = {}229 filter = options[:filter] || "/./"230 filter = Regexp.new $1 if filter =~ %r%/(.*)/%231 filtered_methods = self.runnable_methods.find_all { |m|232 filter === m || filter === "#{self}##{m}"233 }234 exclude = options[:exclude]235 exclude = Regexp.new $1 if exclude =~ %r%/(.*)/%236 filtered_methods.delete_if { |m|237 exclude === m || exclude === "#{self}##{m}"238 }239 return if filtered_methods.empty?240 with_info_handler reporter do241 filtered_methods.each do |method_name|242 run_one_method self, method_name, reporter243 end244 end245 end246 ##247 # Runs a single method and has the reporter record the result.248 # This was considered internal API but is factored out of run so249 # that subclasses can specialize the running of an individual250 # test. See Minitest::ParallelTest::ClassMethods for an example.251 def self.run_one_method klass, method_name, reporter252 reporter.prerecord klass, method_name253 reporter.record Minitest.run_one_method(klass, method_name)254 end255 def self.with_info_handler reporter, &block # :nodoc:256 handler = lambda do257 unless reporter.passed? then258 warn "Current results:"259 warn ""260 warn reporter.reporters.first261 warn ""262 end263 end264 on_signal ::Minitest.info_signal, handler, &block265 end266 SIGNALS = Signal.list # :nodoc:267 def self.on_signal name, action # :nodoc:268 supported = SIGNALS[name]269 old_trap = trap name do270 old_trap.call if old_trap.respond_to? :call271 action.call272 end if supported273 yield274 ensure275 trap name, old_trap if supported276 end277 ##278 # Each subclass of Runnable is responsible for overriding this279 # method to return all runnable methods. See #methods_matching.280 def self.runnable_methods281 raise NotImplementedError, "subclass responsibility"282 end283 ##284 # Returns all subclasses of Runnable.285 def self.runnables286 @@runnables287 end288 @@marshal_dump_warned = false289 def marshal_dump # :nodoc:290 unless @@marshal_dump_warned then291 warn ["Minitest::Runnable#marshal_dump is deprecated.",292 "You might be violating internals. From", caller.first].join " "293 @@marshal_dump_warned = true294 end295 [self.name, self.failures, self.assertions, self.time]296 end297 def marshal_load ary # :nodoc:298 self.name, self.failures, self.assertions, self.time = ary299 end300 def failure # :nodoc:301 self.failures.first302 end303 def initialize name # :nodoc:304 self.name = name305 self.failures = []306 self.assertions = 0307 end308 ##309 # Runs a single method. Needs to return self.310 def run311 raise NotImplementedError, "subclass responsibility"312 end313 ##314 # Did this run pass?315 #316 # Note: skipped runs are not considered passing, but they don't317 # cause the process to exit non-zero.318 def passed?319 raise NotImplementedError, "subclass responsibility"320 end321 ##322 # Returns a single character string to print based on the result323 # of the run. Eg ".", "F", or "E".324 def result_code325 raise NotImplementedError, "subclass responsibility"326 end327 ##328 # Was this run skipped? See #passed? for more information.329 def skipped?330 raise NotImplementedError, "subclass responsibility"331 end332 end333 ##334 # Shared code for anything that can get passed to a Reporter. See335 # Minitest::Test & Minitest::Result.336 module Reportable337 ##338 # Did this run pass?339 #340 # Note: skipped runs are not considered passing, but they don't341 # cause the process to exit non-zero.342 def passed?343 not self.failure344 end345 ##346 # The location identifier of this test. Depends on a method347 # existing called class_name.348 def location349 loc = " [#{self.failure.location}]" unless passed? or error?350 "#{self.class_name}##{self.name}#{loc}"351 end352 def class_name # :nodoc:353 raise NotImplementedError, "subclass responsibility"354 end355 ##356 # Returns ".", "F", or "E" based on the result of the run.357 def result_code358 self.failure and self.failure.result_code or "."359 end360 ##361 # Was this run skipped?362 def skipped?363 self.failure and Skip === self.failure364 end365 ##366 # Did this run error?367 def error?368 self.failures.any? { |f| UnexpectedError === f }369 end370 end371 ##372 # This represents a test result in a clean way that can be373 # marshalled over a wire. Tests can do anything they want to the374 # test instance and can create conditions that cause Marshal.dump to375 # blow up. By using Result.from(a_test) you can be reasonably sure376 # that the test result can be marshalled.377 class Result < Runnable378 include Minitest::Reportable379 undef_method :marshal_dump380 undef_method :marshal_load381 ##382 # The class name of the test result.383 attr_accessor :klass384 ##385 # The location of the test method.386 attr_accessor :source_location387 ##388 # Create a new test result from a Runnable instance.389 def self.from runnable390 o = runnable391 r = self.new o.name392 r.klass = o.class.name393 r.assertions = o.assertions394 r.failures = o.failures.dup395 r.time = o.time396 r.source_location = o.method(o.name).source_location rescue ["unknown", -1]397 r398 end399 def class_name # :nodoc:400 self.klass # for Minitest::Reportable401 end402 def to_s # :nodoc:403 return location if passed? and not skipped?404 failures.map { |failure|405 "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"406 }.join "\n"407 end408 end409 ##410 # Defines the API for Reporters. Subclass this and override whatever411 # you want. Go nuts.412 class AbstractReporter413 include Mutex_m414 ##415 # Starts reporting on the run.416 def start417 end418 ##419 # About to start running a test. This allows a reporter to show420 # that it is starting or that we are in the middle of a test run.421 def prerecord klass, name422 end423 ##424 # Record a result and output the Runnable#result_code. Stores the425 # result of the run if the run did not pass.426 def record result427 end428 ##429 # Outputs the summary of the run.430 def report431 end432 ##433 # Did this run pass?434 def passed?435 true436 end437 end438 class Reporter < AbstractReporter # :nodoc:439 ##440 # The IO used to report.441 attr_accessor :io442 ##443 # Command-line options for this run.444 attr_accessor :options445 def initialize io = $stdout, options = {} # :nodoc:446 super()447 self.io = io448 self.options = options449 end450 end451 ##452 # A very simple reporter that prints the "dots" during the run.453 #454 # This is added to the top-level CompositeReporter at the start of455 # the run. If you want to change the output of minitest via a456 # plugin, pull this out of the composite and replace it with your457 # own.458 class ProgressReporter < Reporter459 def prerecord klass, name #:nodoc:460 if options[:verbose] then461 io.print "%s#%s = " % [klass.name, name]462 io.flush463 end464 end465 def record result # :nodoc:466 io.print "%.2f s = " % [result.time] if options[:verbose]467 io.print result.result_code468 io.puts if options[:verbose]469 end470 end471 ##472 # A reporter that gathers statistics about a test run. Does not do473 # any IO because meant to be used as a parent class for a reporter474 # that does.475 #476 # If you want to create an entirely different type of output (eg,477 # CI, HTML, etc), this is the place to start.478 class StatisticsReporter < Reporter479 # :stopdoc:480 attr_accessor :assertions481 attr_accessor :count482 attr_accessor :results483 attr_accessor :start_time484 attr_accessor :total_time485 attr_accessor :failures486 attr_accessor :errors487 attr_accessor :skips488 # :startdoc:489 def initialize io = $stdout, options = {} # :nodoc:490 super491 self.assertions = 0492 self.count = 0493 self.results = []494 self.start_time = nil495 self.total_time = nil496 self.failures = nil497 self.errors = nil498 self.skips = nil499 end500 def passed? # :nodoc:501 results.all?(&:skipped?)502 end503 def start # :nodoc:504 self.start_time = Minitest.clock_time505 end506 def record result # :nodoc:507 self.count += 1508 self.assertions += result.assertions509 results << result if not result.passed? or result.skipped?510 end511 def report # :nodoc:512 aggregate = results.group_by { |r| r.failure.class }513 aggregate.default = [] # dumb. group_by should provide this514 self.total_time = Minitest.clock_time - start_time515 self.failures = aggregate[Assertion].size516 self.errors = aggregate[UnexpectedError].size517 self.skips = aggregate[Skip].size518 end519 end520 ##521 # A reporter that prints the header, summary, and failure details at522 # the end of the run.523 #524 # This is added to the top-level CompositeReporter at the start of525 # the run. If you want to change the output of minitest via a526 # plugin, pull this out of the composite and replace it with your527 # own.528 class SummaryReporter < StatisticsReporter529 # :stopdoc:530 attr_accessor :sync531 attr_accessor :old_sync532 # :startdoc:533 def start # :nodoc:534 super535 io.puts "Run options: #{options[:args]}"536 io.puts537 io.puts "# Running:"538 io.puts539 self.sync = io.respond_to? :"sync=" # stupid emacs540 self.old_sync, io.sync = io.sync, true if self.sync541 end542 def report # :nodoc:543 super544 io.sync = self.old_sync545 io.puts unless options[:verbose] # finish the dots546 io.puts547 io.puts statistics548 aggregated_results io549 io.puts summary550 end551 def statistics # :nodoc:552 "Finished in %.6fs, %.4f runs/s, %.4f assertions/s." %553 [total_time, count / total_time, assertions / total_time]554 end555 def aggregated_results io # :nodoc:556 filtered_results = results.dup557 filtered_results.reject!(&:skipped?) unless options[:verbose]558 filtered_results.each_with_index { |result, i|559 io.puts "\n%3d) %s" % [i+1, result]560 }561 io.puts562 io563 end564 def to_s # :nodoc:565 aggregated_results(StringIO.new(binary_string)).string566 end567 def summary # :nodoc:568 extra = ""569 extra = "\n\nYou have skipped tests. Run with --verbose for details." if570 results.any?(&:skipped?) unless options[:verbose] or ENV["MT_NO_SKIP_MSG"]571 "%d runs, %d assertions, %d failures, %d errors, %d skips%s" %572 [count, assertions, failures, errors, skips, extra]573 end574 private575 if '<3'.respond_to? :b576 def binary_string; ''.b; end577 else578 def binary_string; ''.force_encoding(Encoding::ASCII_8BIT); end579 end580 end581 ##582 # Dispatch to multiple reporters as one.583 class CompositeReporter < AbstractReporter584 ##585 # The list of reporters to dispatch to.586 attr_accessor :reporters587 def initialize *reporters # :nodoc:588 super()589 self.reporters = reporters590 end591 def io # :nodoc:592 reporters.first.io593 end594 ##595 # Add another reporter to the mix.596 def << reporter597 self.reporters << reporter598 end599 def passed? # :nodoc:600 self.reporters.all?(&:passed?)601 end602 def start # :nodoc:603 self.reporters.each(&:start)604 end605 def prerecord klass, name # :nodoc:606 self.reporters.each do |reporter|607 # TODO: remove conditional for minitest 6608 reporter.prerecord klass, name if reporter.respond_to? :prerecord609 end610 end611 def record result # :nodoc:612 self.reporters.each do |reporter|613 reporter.record result614 end615 end616 def report # :nodoc:617 self.reporters.each(&:report)618 end619 end620 ##621 # Represents run failures.622 class Assertion < Exception623 def error # :nodoc:624 self625 end626 ##627 # Where was this run before an assertion was raised?628 def location629 last_before_assertion = ""630 self.backtrace.reverse_each do |s|631 break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/632 last_before_assertion = s633 end634 last_before_assertion.sub(/:in .*$/, "")635 end636 def result_code # :nodoc:637 result_label[0, 1]638 end639 def result_label # :nodoc:640 "Failure"641 end642 end643 ##644 # Assertion raised when skipping a run.645 class Skip < Assertion646 def result_label # :nodoc:647 "Skipped"648 end649 end650 ##651 # Assertion wrapping an unexpected error that was raised during a run.652 class UnexpectedError < Assertion653 attr_accessor :exception # :nodoc:654 def initialize exception # :nodoc:655 super "Unexpected exception"656 self.exception = exception657 end658 def backtrace # :nodoc:659 self.exception.backtrace660 end661 def error # :nodoc:662 self.exception663 end664 def message # :nodoc:665 bt = Minitest.filter_backtrace(self.backtrace).join "\n "666 "#{self.exception.class}: #{self.exception.message}\n #{bt}"667 end668 def result_label # :nodoc:669 "Error"670 end671 end672 ##673 # Provides a simple set of guards that you can use in your tests674 # to skip execution if it is not applicable. These methods are675 # mixed into Test as both instance and class methods so you676 # can use them inside or outside of the test methods.677 #678 # def test_something_for_mri679 # skip "bug 1234" if jruby?680 # # ...681 # end682 #683 # if windows? then684 # # ... lots of test methods ...685 # end686 module Guard687 ##688 # Is this running on jruby?689 def jruby? platform = RUBY_PLATFORM690 "java" == platform691 end692 ##693 # Is this running on maglev?694 def maglev? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE695 "maglev" == platform696 end697 ##698 # Is this running on mri?699 def mri? platform = RUBY_DESCRIPTION700 /^ruby/ =~ platform701 end702 ##703 # Is this running on rubinius?704 def rubinius? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE705 "rbx" == platform706 end707 ##708 # Is this running on windows?709 def windows? platform = RUBY_PLATFORM710 /mswin|mingw/ =~ platform711 end712 end713 ##714 # The standard backtrace filter for minitest.715 #716 # See Minitest.backtrace_filter=.717 class BacktraceFilter718 MT_RE = %r%lib/minitest% #:nodoc:719 ##720 # Filter +bt+ to something useful. Returns the whole thing if $DEBUG.721 def filter bt722 return ["No backtrace"] unless bt723 return bt.dup if $DEBUG724 new_bt = bt.take_while { |line| line !~ MT_RE }725 new_bt = bt.select { |line| line !~ MT_RE } if new_bt.empty?726 new_bt = bt.dup if new_bt.empty?727 new_bt728 end729 end730 self.backtrace_filter = BacktraceFilter.new731 def self.run_one_method klass, method_name # :nodoc:732 result = klass.new(method_name).run733 raise "#{klass}#run _must_ return a Result" unless Result === result734 result735 end736 # :stopdoc:737 if defined? Process::CLOCK_MONOTONIC # :nodoc:738 def self.clock_time739 Process.clock_gettime Process::CLOCK_MONOTONIC740 end741 else...
error.rb
Source:error.rb
...55 self.class.name56 end57 def inner_backtrace58 return [] unless cause59 bt = cause.backtrace_locations60 ignored_bt = backtrace_locations61 while bt.last.to_s == ignored_bt.last.to_s62 bt.pop63 ignored_bt.pop64 end65 while bt.last.path == ignored_bt.last.path66 bt.pop67 end68 bt69 end70 end71 include Impl72 def initialize(**context)73 set_context context74 super message75 end76end77class Gel::LoadError < ::LoadError78 include Gel::UserError::Impl79 def initialize(**context)80 set_context context81 super message82 end...
test.rb
Source:test.rb
...168 rescue TypeError169 neuter_exception e170 end171 def neuter_exception e172 bt = e.backtrace173 msg = e.message.dup174 new_exception e.class, msg, bt # e.class can be a problem...175 rescue TypeError176 msg.prepend "Neutered Exception #{e.class}: "177 new_exception RuntimeError, msg, bt # but if this raises, we die178 end179 def new_exception klass, msg, bt180 ne = klass.new msg181 ne.set_backtrace bt182 Marshal.dump ne # can raise TypeError183 ne184 end185 def with_info_handler &block # :nodoc:186 t0 = Minitest.clock_time187 handler = lambda do188 warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Minitest.clock_time - t0]189 end190 self.class.on_signal ::Minitest.info_signal, handler, &block191 end192 include LifecycleHooks193 include Guard194 extend Guard195 end # Test...
bt
Using AI Code Generation
1 def initialize(name, age)2p = Person.new("Evan", 30)3The caller method accepts an optional parameter, which is the number of calls to include in the array. If you call caller(0) , it returns the entire backtrace. If you call caller(1) , it returns a backtrace that is one call long. If you call caller(2) , it returns a backtrace that is two calls long, and so on. The following program demonstrates this:4 def initialize(name, age)5p = Person.new("Evan", 30)
bt
Using AI Code Generation
1p = Person.new("Evan", 30)2The caller method accepts an optional parameter, which is the number of calls to include in the array. If you call caller(0) , it returns the entire backtrace. If you call caller(1) , it returns a backtrace that is one call long. If you call caller(2) , it returns a backtrace that is two calls long, and so on. The following program demonstrates this:3 def initialize(name, age)4p = Person.new("Evan", 30)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!