Minitest Tips

Minitestで各テストの最初、最後、全てのテストの最後に実行したい処理は次のように書ける。

require 'minitest/autorun'

class XxxTest < Minitest::Test

  # 全てのテストが終わった後に処理される
  Minitest.after_run do
    p "after_run"
  end

  # 各テストの最初に実行される
  def setup
    p "setup"
  end

  # 各テストの最後に実行される
  def teardown
    p "teardown"
  end

  def test_xxx
    ...
  end

  def test_xxx
    ...
  end
end

実行結果

% bundle exec ruby test/xxx_test.rb
Run options: --seed 26197

# Running:

"setup"
"teardown"
."setup"
"teardown"
.

Finished in 0.026552s, 112.9858 runs/s, 37.6619 assertions/s.

2 runs, 1 assertions, 0 failures, 0 errors, 0 skips
"after_run"