class Minitest::Test

Subclass Test to create your own tests. Typically you’ll want a Test subclass per implementation class.

See Minitest::Assertions

Public Class Methods

Declare the current class an abstract test case that doesn’t run tests except in subclasses.

Example:

class AbstractTestCase < Minitest::Test
  abstract_test_case!

  attr_accessor :obj

  def test_method
    assert_equal 42, obj.method
  end
end

class TestConcrete < AbstractTestCase
  def setup = self.obj = Concrete.new
end
# File lib/minitest/test.rb, line 46
def self.abstract_test_case! klass = self
  extend Module.new {
    define_method :run do |*args|
      super(*args) unless self == klass
    end
  }
end

Call this at the top of your tests when you absolutely positively need to have ordered tests.

# File lib/minitest/test.rb, line 58
def self.i_suck_and_my_tests_are_order_dependent!
  class << self
    undef_method :run_order if method_defined? :run_order
    define_method :run_order do :alpha end
  end
end

Make diffs for this Test use pretty_inspect so that diff in Assertions#assert_equal can have more details.

NOTE: this is much slower than the regular inspect but much more usable for complex objects.

Example:

class TestBlah < Minitest::Test
  make_my_diffs_pretty!

  def test_complex_object
    exp = deep_complex_object
    assert_equal exp, obj.actual # outputs clean diff
  end
end
# File lib/minitest/test.rb, line 83
def self.make_my_diffs_pretty!
  require "pp"

  define_method :mu_pp, &:pretty_inspect
end

Call this at the top of your tests (inside the Minitest::Test subclass or describe block) when you want to run your tests in parallel.

Example:

class TestZeroSideEffects < Minitest::Test
  parallelize_me!

  # ... tests ...
end
# File lib/minitest/test.rb, line 102
def self.parallelize_me!
  return unless Minitest.parallel_executor
  include Minitest::Parallel::Test
  extend Minitest::Parallel::Test::ClassMethods
end

Returns all public instance methods starting with “test_”. Based on run_order, the methods are either sorted, randomized (default), or run in parallel.

# File lib/minitest/test.rb, line 113
def self.runnable_methods
  methods = methods_matching(/^test_/)

  case self.run_order
  when :random, :parallel then
    srand Minitest.seed
    methods.sort.shuffle
  when :alpha, :sorted then
    methods.sort
  else
    raise "Unknown_order: %p" % [self.run_order]
  end
end

Public Instance Methods

Runs a single test with setup/teardown hooks. Override this method to customize how the test is run on a class-by-class basis.

Example:

class TestBlah < Minitest::Test
  def run
    Dir.chdir tmp_dir do
      super
    end
  end
end
# File lib/minitest/test.rb, line 142
def run
  time_it do
    capture_exceptions do
      SETUP_METHODS.each do |hook|
        self.send hook
      end

      self.send self.name
    end

    TEARDOWN_METHODS.each do |hook|
      capture_exceptions do
        self.send hook
      end
    end
  end

  Result.from self # per contract
end