Minitest assertions

The class under test

A test needs something with a rule worth checking. The calculator below has one normal path and one error path:

class Calculator def add(a, b) = a + b def divide(a, b) raise ArgumentError, "division by zero" if b.zero? a / b end end

The library is plain Ruby, so the syntax check passes before a test exists:

ruby -c lib/calculator.rb

Syntax OK

assert and refute

A test class inherits from Minitest::Test, and every method that starts with test_ is one test. assert_equal takes the expected value first, which is what the failure message prints. Install the gem first:

gem install minitest

2 gems installed

require "minitest/autorun" require_relative "../lib/calculator" class CalculatorTest < Minitest::Test def test_add assert_equal 4, Calculator.new.add(2, 2) end def test_not_nil refute_nil Calculator.new end end

refute is assert with the condition inverted, and refute_nil is the common case with a better message. The one-assertion rule is a guideline: a test with five assertions fails on the first and hides the others, so each test names one behaviour.

assert_raises

The error path is a test too. assert_raises takes the class, runs the block and returns the exception, so the message can be checked as well:

def test_divide_by_zero error = assert_raises(ArgumentError) { Calculator.new.divide(1, 0) } assert_equal "division by zero", error.message end

Without assert_raises the exception escapes and the test is reported as an error, not a failure. The difference matters: a failure is a wrong value, an error is code that did not return.

setup and teardown

setup runs before every test and teardown after it, whatever the test did. The instance variable is fresh for each test, which is what stops one test from depending on another:

require "minitest/autorun" require_relative "../lib/calculator" class CalculatorTest < Minitest::Test def setup @calc = Calculator.new end def teardown @calc = nil end def test_add assert_equal 4, @calc.add(2, 2) end end

Running it

ruby test/calc_test.rb

... Finished in 0.001132s, 2649.860 runs/s, 3533.147 assertions/s. 3 runs, 4 assertions, 0 failures, 0 errors, 0 skips

Three tests, four assertions: the error test makes two. Every line of the summary is read by a person or a parser, and the exit status is zero only when failures and errors are both zero.

A single test is run by name: ruby test/calc_test.rb -n test_add, and a whole suite by a glob: ruby -Itest test/*_test.rb.

What to watch for

A test that asserts the current output of a bug is a test that protects the bug. Write the expected value from the rule, not from a run.

Random order is a feature: a suite that only passes in one order has a test that changes shared state. Minitest shuffles per run, and the seed in the output is the way to repeat a failing order.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? class Calculator def add(a, b) = a + b def divide(a, b) raise ArgumentError, "division by zero" if b.zero? a / b end end calc = Calculator.new puts calc.add(2, 2) puts calc.divide(8, 2)

    Answer

    4 / 4

    The two normal paths return 4 in each case, which is what the tests assert.

  2. What does this program print? class Calculator def divide(a, b) raise ArgumentError, "division by zero" if b.zero? a / b end end begin Calculator.new.divide(1, 0) rescue ArgumentError => e puts e.message end

    Answer

    division by zero

    The guard raises with that message, and the rescue prints the message it carries.

  3. Which argument of assert_equal is the expected value?

    Answer

    The first one

    The expected value comes first, which is why the failure message reads expected then actual.

  4. What does refute_nil Calculator.new check?

    Answer

    That the value is not nil

    refute inverts the assertion, and refute_nil is the common case with its own message.

  5. What does assert_raises(ArgumentError) { ... } do?

    Answer

    Runs the block and fails if it does not raise that error

    It catches the expected error and returns the exception, so the message can be checked too.

  6. What does setup do in a Minitest class?

    Answer

    Runs before every test, giving each one a fresh instance

    A fresh instance per test is what stops one test from depending on another.

  7. What is the difference between a failure and an error in a test run?

    Answer

    A failure is a wrong value, an error is code that did not return

    An assertion reports a failure, while an escaped exception is reported as an error.

  8. When is the exit status of a Minitest run zero?

    Answer

    When failures and errors are both zero

    The status is what a CI job reads, so a failure or an error has to change it.

  9. Why does the page warn against asserting the current output of a bug?

    Answer

    The test then protects the bug instead of the rule

    The expected value comes from the rule, not from what the code happens to do today.

  10. Why is a suite that only passes in one order a problem?

    Answer

    One test changes state another test depends on

    Random order exposes shared state, and the seed lets a developer repeat the failing order.

Article author: Arthur Isaev