Testing with Minitest
Minitest is Ruby's built-in testing framework. It's fast, simple, and powerful. You don't need to install anything extra โ it comes with Ruby. Minitest lets you write tests that verify your code works correctly.
require "minitest/autorun"
class TestCalculator < Minitest::Test
def test_addition
assert_equal 4, 2 + 2
end
def test_subtraction
assert_equal 5, 10 - 5
end
def test_division
assert_equal 3, 10 / 3 # integer division
end
end
Try it Yourself ->
Assert Methods
Minitest provides many assert methods for different checks. Use the most specific one for clear test failure messages. The assertion library is your best friend when debugging.
require "minitest/autorun"
class TestAssertions < Minitest::Test
def test_equality
assert_equal 4, 2 + 2
assert_equal "hello", "hello"
end
def test_truthiness
assert_nil nil
assert_nil false
assert_empty []
assert_empty ""
end
def test_types
assert_instance_of String, "hello"
assert_kind_of Object, "hello"
assert_respond_to "hello", :length
end
def test_errors
assert_raises(ZeroDivisionError) { 1 / 0 }
assert_raises(ArgumentError) { raise ArgumentError }
end
def test_refutation
refute_equal 1, 2
refute_nil "not nil"
refute_empty [1, 2, 3]
end
end
Try it Yourself ->
Setup and Teardown
setup runs before each test. teardown runs after each test. Use them to prepare test data and clean up. This keeps your tests independent and repeatable.
require "minitest/autorun"
class TestUser < Minitest::Test
def setup
@user = User.new("Alice", "alice@example.com")
@other = User.new("Bob", "bob@example.com")
end
def teardown
@user = nil
@other = nil
end
def test_name
assert_equal "Alice", @user.name
end
def test_email
assert_equal "alice@example.com", @user.email
end
def test_equality
refute_equal @user, @other
end
end
class User
attr_reader :name, :email
def initialize(name, email)
@name = name
@email = email
end
end
Try it Yourself ->
Test::Unit Style
Minitest supports two styles: spec style (describe/it) and test style (class/method). Both work equally well. Pick the one that feels natural to you.
require "minitest/autorun"
# Spec style
describe "String" do
it "can be reversed" do
"hello".reverse.must_equal "olleh"
end
it "has a length" do
"hello".length.must_equal 5
end
it "can be upcased" do
"hello".upcase.must_equal "HELLO"
end
end
# Test style
class StringTest < Minitest::Test
def test_reverse
assert_equal "olleh", "hello".reverse
end
def test_length
assert_equal 5, "hello".length
end
end
Try it Yourself ->
Why Minitest and RSpec
Minitest is fast and minimal โ it does one thing well. RSpec is more expressive with a rich DSL. Minitest is great for simple projects. RSpec shines in large codebases with complex test scenarios. Both are excellent choices.
# Minitest: fast, simple, built-in
# RSpec: expressive, feature-rich, external
# Run Minitest
# ruby test/my_test.rb
# Run with rake
# rake test
# RSpec equivalent:
# describe Calculator do
# it "adds numbers" do
# expect(Calculator.add(2, 2)).to eq(4)
# end
# end
# Minitest equivalent:
class CalculatorTest < Minitest::Test
def test_add
assert_equal 4, Calculator.add(2, 2)
end
end
Try it Yourself ->