Testing with Minitest

Minitest is Ruby's built-in testing framework that lets you verify your code automatically with concise assertions, so you catch bugs before your users do.

Learn Testing with Minitest in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll write test classes and assertions, use setup , check for raised errors, and try the spec style.

What You'll Learn in This Lesson

1️⃣ Your First Test

A Minitest test is a class that inherits from Minitest::Test . Every method whose name starts with test_ is a test. Inside, call assertions like assert_equal(expected, actual) or assert(condition) . The line require 'minitest/autorun' makes the file run its tests automatically.

2️⃣ More Assertions, setup & assert_raises

setup runs before every test_ method, giving each test fresh, isolated state. Beyond assert_equal there's assert_nil , assert_includes , and assert_raises , which passes only if the block raises the expected exception.

3️⃣ Spec Style & Minitest vs RSpec

Minitest also offers a spec style that reads like a sentence: describe groups tests, it defines one, and expectations use _(value).must_equal expected . It's a lighter alternative to RSpec's expect(...).to DSL, with no extra gem to install.

Your turn. Complete two assertions about strings so the suite passes. Fill in each ___ .

Build a tiny Stack and write a Minitest suite for it with a setup , an equality assertion, and an assert_raises . This mirrors how you'd test a real class. Run with ruby test_stack.rb .

📋 Quick Reference — Minitest

Practice quiz

How is Minitest distributed?

  • a paid gem
  • part of Rails only
  • built into Ruby, no install needed
  • a separate download

Answer: built into Ruby, no install needed. Minitest ships with Ruby itself.

What does require 'minitest/autorun' do?

  • registers an at_exit hook that runs the tests automatically
  • installs Minitest
  • imports assertions only
  • starts a web server

Answer: registers an at_exit hook that runs the tests automatically. autorun runs all defined tests when the file finishes loading.

A classic-style test class inherits from:

  • Minitest::Spec
  • Test::Unit
  • RSpec::Test
  • Minitest::Test

Answer: Minitest::Test. xUnit-style tests subclass Minitest::Test.

Which method names are run as tests in a Minitest::Test class?

  • any method
  • methods starting with test_
  • methods ending in _test
  • methods named check

Answer: methods starting with test_. Only methods whose names start with test_ are run.

What is the correct argument order for assert_equal?

  • assert_equal expected, actual
  • assert_equal actual, expected
  • order does not matter
  • assert_equal value only

Answer: assert_equal expected, actual. assert_equal takes expected first, then actual.

When does plain assert(value) pass?

  • when value is nil
  • only when value is true
  • when value is truthy (not nil or false)
  • when value is zero

Answer: when value is truthy (not nil or false). assert passes for any truthy value.

Which assertion checks that a block raises an error?

  • assert_error
  • assert_raises
  • assert_throws
  • assert_fails

Answer: assert_raises. assert_raises(Error) { ... } passes when the block raises that error.

What does the setup method do?

  • runs once before all tests
  • runs after each test
  • configures the seed
  • runs before every test_ method for fresh state

Answer: runs before every test_ method for fresh state. setup runs before each test, giving isolated state.

In Minitest spec style, which pair defines a test group and a test?

  • context / specify
  • describe / it
  • suite / case
  • group / test

Answer: describe / it. Spec style uses describe to group and it to define a test.

Compared to RSpec, Minitest is generally:

  • larger with a big matcher DSL
  • browser-based
  • small, built-in, and dependency-free
  • only for Rails

Answer: small, built-in, and dependency-free. Minitest favours simplicity and ships with Ruby; RSpec is a larger external gem.