liveBook · Manning

Test

As explained in The Well-Grounded Rubyist, Fourth Edition

A test method is a method whose name begins with test and that contains executable assertions for a test case.

A test method is a method whose name begins with `test` and contains executable assertions. In MiniTest, define it in a class that inherits from `Minitest::Test`; assertions determine whether the test passes. With `minitest/autorun`, test methods run automatically. section 15.5.1

Testing a card deck

After dealing one card, `test_deal_one` checks that the deck has 51 cards. After dealing five, `test_deal_many` checks for 47. A `setup` method creates a fresh deck before each test. section 15.5.1

Listing 15.6 card_test.rb: Testing the dealing accuracy of PlayingCards::Deck
require 'minitest/autorun'     1
require_relative 'cards'
class CardTest < Minitest::Test    2
  def setup                           3
    @deck = PlayingCards::Deck.new
  end
  def test_deal_one         4
    @deck.deal
    assert_equal(51, @deck.size)       5
  end
  def test_deal_many                    6
    @deck.deal(5)
    assert_equal(47, @deck.size)
  end
end

See the complete MiniTest example, including the test class, setup method, and assertions. section 15.5.1

A small testing utility can emulate this pattern by using `inherited` and `method_added` callbacks to detect newly defined test methods, create an instance, run setup if present, and call the test. section 15.5.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains MiniTest naming, setup, and autorun, then implements test discovery with inheritance and method-added callbacks.
Examples
Tests one-card and five-card deals by asserting the deck’s remaining size.
What you can do after
Apply it by writing MiniTest methods that assert expected behavior and run automatically.
How it connects
Connects test methods to assertions, setup methods, inheritance, and method-added callbacks.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.