How do I write my first Ruby test with Minitest?
Create a file that requires minitest/autorun and your code, define a class that inherits from Minitest::Test, and write methods whose names start with test_. Inside each one, call an assertion such as assert_equal with the expected value first. Run the file with ruby, and Minitest prints how many tests passed or failed.
Cite
Poignant Guide Ruby Q&A, "How do I write my first Ruby test with Minitest?", https://poignantguide.net/answers/read/how-do-i-write-my-first-ruby-test-with-minitest/, accessed 2026-11-27.- Fenwick HaleStaffStaff: gems, Rails, tooling and debugging ·
What Minitest is
A test is a small program that calls your code and checks the answer, so you find out about a broken change in seconds instead of by accident. Minitest is a testing library for Ruby. Its README describes it as a small, fast unit testing framework with a rich set of assertions, and notes that it does not reinvent anything Ruby already provides: tests are ordinary classes and methods, so learning Ruby is most of what you need.
An assertion is a method that checks a claim and raises a failure if the claim is false. The Minitest assertions documentation lists them all.
A minimal example
Put this in
lib/calculator.rb:class Calculator def add(a, b) a + b end def divide(a, b) raise ArgumentError, "cannot divide by zero" if b.zero? a / b end endPut this in
test/calculator_test.rb:require "minitest/autorun" require_relative "../lib/calculator" class CalculatorTest < Minitest::Test def setup @calc = Calculator.new end def test_add_returns_the_sum assert_equal 5, @calc.add(2, 3) end def test_divide_by_zero_raises error = assert_raises(ArgumentError) { @calc.divide(1, 0) } assert_equal "cannot divide by zero", error.message end endRun it from the project folder:
$ ruby test/calculator_test.rb Run options: --seed 12296 # Running: .. Finished in 0.000468s, 4277.6997 runs/s, 6416.5495 assertions/s. 2 runs, 3 assertions, 0 failures, 0 errors, 0 skipsEach dot is one passing test. The seed number controls the random order the tests ran in; it will differ on your machine.
What a failure looks like
Change
a + btoa - bin the calculator and run again:$ ruby test/calculator_test.rb F. 1) Failure: CalculatorTest#test_add_returns_the_sum [test/calculator_test.rb:10]: Expected: 5 Actual: -1 2 runs, 3 assertions, 1 failures, 0 errors, 0 skipsThe message names the test, the file and line, the value you expected and the value you got. A failure means an assertion was false. An error means your code raised an exception, such as a typo or a nil, before the assertion could run.
Why it works
require "minitest/autorun"loads the library and tells it to run every test class when the file ends, so there is no separate runner to start. Minitest finds tests by name: any method beginning withtest_in a subclass of Minitest::Test. Before each test it callssetup, which gives every test a fresh Calculator. The README says there is "no magic involved for test-case discovery".The assertions you will use first
Assertion Passes when assert_equal expected, actualThe two values are equal with ==assert_nil valueThe value is nil assert_includes collection, itemThe collection contains the item assert_raises(Error) { ... }The block raises that error; it returns the exception so you can check its message assert_in_delta expected, actual, 0.01Two floats are within a small distance, because float math is inexact refute_*versionsThe opposite, for example refute_nilMistakes beginners make
- A method name that does not start with
test_. Minitest silently ignores it, so a test you thought was running never runs. If the count in the summary is lower than you expect, check the names. - Swapping expected and actual. The expected value goes first. If you swap them, the failure message labels the numbers backwards and sends you in the wrong direction.
- Comparing floats with assert_equal. Use
assert_in_delta, as the documentation recommends.
Check this in order when a test will not run
- Does the file require
minitest/autorun? - Does the class inherit from
Minitest::Test? - Does every test method start with
test_? - Does
require_relativepoint at the right file? A LoadError means the path is wrong. - Are you running from the project folder, so
test/...resolves? - Under Bundler (
bundle exec), isgem "minitest"in your Gemfile? The README says a missing entry causes an uninitialized constant error for the Test class.
Running more than one file
When you have several test files, add a Rakefile as the README shows:
require "minitest/test_task" Minitest::TestTask.create task default: :testThen
rake testruns them all. This needs a recent Minitest; the README lists Ruby 3.2 or newer. Checked on 12 November 2026.Tests lean on classes, so the thread on how attr_accessor, attr_reader and attr_writer work helps if the Calculator code looked new. If a test errors with nil, see undefined method for nil:NilClass and use binding.irb inside the test to look around. The guide's chapter 5 covers classes and objects.
- A method name that does not start with
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- Odile MarchettiStaffStaff: Ruby basics, objects and the Poignant Guide ·
A tip from teaching this: write the test before the method, watch it fail with the expected-and-actual message, then make it pass. Seeing that red message once makes the green dots mean something. Start with one tiny test for one tiny method and add the next only when the first passes.