MiniTest
02 / 02

MiniTest Fundamentals: Test Style, Assertions & setup/teardown

MiniTest: Test Style, Assertions & setup/teardown

MiniTest is a lightweight testing framework shipped in Ruby's standard library. Rails uses it as the default (via `rails test`), and it supports both a classic xUnit-style approach and an RSpec-inspired spec style.

Classic Test Style

class UserTest < Minitest::Test
  def setup
    @user = User.new(name: "Ada")
  end

  def test_name_is_set
    assert_equal "Ada", @user.name
  end

  def test_invalid_without_name
    @user.name = nil
    refute @user.valid?
  end
end

Core Assertions

assert_equal(expected, actual)
assert_nil(value)
assert_includes(collection, item)
assert_raises(ArgumentError) { risky_call }

# Each has a clearer, more specific failure message than the
# generic assert(condition) would produce for the same check

setup / teardown

setup runs before every test method, teardown after. They keep each test starting from a known, consistent baseline and clean up afterward -- important for avoiding one test's leftover state leaking into another.

Random Test Order

MiniTest runs tests in random order by default, specifically to surface accidental coupling between tests -- a test that only passes because an earlier test left behind some state will fail intermittently instead of silently passing.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free