In Files

  • minitest/unit.rb

MiniTest::Unit

Public Class Methods

after_tests() click to toggle source

A simple hook allowing you to run a block of code after the tests are done. Eg:

MiniTest::Unit.after_tests { p $debugging_info }
 
               # File minitest/unit.rb, line 645
def self.after_tests
  at_exit { at_exit { yield } }
end
            
autorun() click to toggle source

Registers MiniTest::Unit to run tests at process exit

 
               # File minitest/unit.rb, line 652
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() click to toggle source

Returns the stream to use for output.

DEPRECATED: use ::output instead.

 
               # File minitest/unit.rb, line 681
def self.out
  warn "::out deprecated, use ::output instead." if $VERBOSE
  output
end
            
output() click to toggle source

Returns the stream to use for output.

 
               # File minitest/unit.rb, line 672
def self.output
  @@out
end
            
output=(stream) click to toggle source

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

 
               # File minitest/unit.rb, line 690
def self.output= stream
  @@out = stream
end
            
plugins() click to toggle source

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

 
               # File minitest/unit.rb, line 714
def self.plugins
  @@plugins ||= (["run_tests"] +
                 public_instance_methods(false).
                 grep(/^run_/).map { |s| s.to_s }).uniq
end
            
runner() click to toggle source

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

 
               # File minitest/unit.rb, line 707
def self.runner
  @@runner ||= self.new
end
            
runner=(runner) click to toggle source

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

 
               # File minitest/unit.rb, line 698
def self.runner= runner
  @@runner = runner
end
            

Public Instance Methods

_run(args = []) click to toggle source

Top level driver, controls all output and filtering.

 
               # File minitest/unit.rb, line 890
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) click to toggle source
 
               # File minitest/unit.rb, line 732
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) click to toggle source
 
               # File minitest/unit.rb, line 773
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) click to toggle source
 
               # File minitest/unit.rb, line 769
def _run_suites suites, type
  suites.map { |suite| _run_suite suite, type }
end
            
options() click to toggle source
 
               # File minitest/unit.rb, line 632
def options
  @options ||= {}
end
            
output() click to toggle source
 
               # File minitest/unit.rb, line 720
def output
  self.class.output
end
            
process_args(args = []) click to toggle source
 
               # File minitest/unit.rb, line 837
def process_args args = []
  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." 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) click to toggle source

Writes status for failed test meth in klass which finished with exception e

 
               # File minitest/unit.rb, line 813
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
            
run(args = []) click to toggle source

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

 
               # File minitest/unit.rb, line 883
def run args = []
  self.class.runner._run(args)
end
            
run_tests() click to toggle source

Runs test suites matching filter.

 
               # File minitest/unit.rb, line 908
def run_tests
  _run_anything :test
end
            
status(io = self.output) click to toggle source

Writes status to io

 
               # File minitest/unit.rb, line 915
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
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus