Skip to content
Poignant Guide Ruby Q&A

How do I write my first Ruby test with Minitest?

Staff answerWritten by Fenwick Hale · Testing, style and clean code1 replyupdated

Short answer

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.

Written by Fenwick Hale · Staff: gems, Rails, tooling and debugging ·
CitePoignant 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.
  1. Staff: 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
    end
    

    Put 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
    end
    

    Run 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 skips
    

    Each 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 + b to a - b in 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 skips
    

    The 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 with test_ in a subclass of Minitest::Test. Before each test it calls setup, 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, actual The two values are equal with ==
    assert_nil value The value is nil
    assert_includes collection, item The 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.01 Two floats are within a small distance, because float math is inexact
    refute_* versions The opposite, for example refute_nil

    Mistakes beginners make

    1. 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.
    2. 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.
    3. Comparing floats with assert_equal. Use assert_in_delta, as the documentation recommends.

    Check this in order when a test will not run

    1. Does the file require minitest/autorun?
    2. Does the class inherit from Minitest::Test?
    3. Does every test method start with test_?
    4. Does require_relative point at the right file? A LoadError means the path is wrong.
    5. Are you running from the project folder, so test/... resolves?
    6. Under Bundler (bundle exec), is gem "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: :test
    

    Then rake test runs 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.

    7
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. 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.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.