Testing with Minitest

Your first assertion

Minitest ships with Ruby tooling as the default small test framework. A test class inherits from Minitest::Test and every method starting with test_ runs as one test.

Install it first with the steps in Gems and Bundler.

require "minitest/autorun" class TestMath < Minitest::Test def test_addition assert_equal 4, 2 + 2 end end

ruby -Itest

Test files usually live in a test folder with project code loaded beside them. The minus capital I flag adds that folder to the load path, so require finds project files.

Run one file directly with ruby while developing. Full suites usually run through rake later.

ruby -Itest test/test_math.rb

Run options: --seed 12345

# Running:

.

Finished in 0.0016s, 625.0000 runs/s, 625.0000 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

The seed and the timings change on every run; the last line is the summary that matters. A failure prints the failing test, the expected value and the stack of the assertion instead of the dot.

Describe blocks

describe groups tests under a readable label, and it blocks name single behaviors. Readers see sentences instead of method names.

Assertions work the same inside, so teams mix both styles in one suite.

require "minitest/autorun" describe "calculator" do it "adds two numbers" do assert_equal 4, 2 + 2 end end

Red green refactor

Write a failing test first and watch it go red. Then write the smallest code that turns it green.

Refactor with green tests guarding you: clean the code without changing behavior, and rerun after each step.

def double(n) n * 2 end

Questions

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

  1. Which method of a Minitest test class runs as a test?

    Answer

    Every method whose name starts with test_

    The framework collects the methods with the test_ prefix, which is why the naming rule matters.

  2. What does this program print? def double(n) n * 2 end puts double(21)

    Answer

    42

    The function under test doubles its argument, and the test asserts exactly this value.

  3. What does this program print? def double(n) n * 2 end puts double(0) puts double(-2)

    Answer

    0 / -4

    Multiplication by two works for zero and for a negative number alike.

  4. What does assert_equal 4, 2 + 2 do?

    Answer

    Raises nothing, because the two values are equal

    assert_equal compares its two arguments and passes silently when they are equal.

  5. What does a failing assert_equal 5, 2 + 2 print?

    Answer

    The failing test, the expected value and the stack of the assertion

    The failure report names the test and shows the expected and actual values instead of the dot.

  6. What does ruby -Itest test/test_math.rb do?

    Answer

    Adds test to the load path and runs that file

    The -I flag puts the folder on the load path, so the file can require project code from it.

  7. Which part of a Minitest run changes on every invocation?

    Answer

    The seed and the timings

    The seed orders the tests randomly and the timing measures the run, so the summary line is the stable part.

  8. What does a describe "calculator" do ... end block do?

    Answer

    Groups tests under a readable label, with it naming one behaviour

    The spec style names a group and one behaviour per it block, and the assertions work the same inside.

  9. Which order does red green refactor prescribe?

    Answer

    Write a failing test, make it pass, then clean the code with the test guarding it

    The failing test proves the test can fail, and the green test guards the refactor.

  10. What does this program print? values = [1, 2, 3] puts values.sum

    Answer

    6

    sum adds the numbers, which is the value a test of a total would assert.

Article author: Arthur Isaev