Minitestとは
テストを自動化する際のテスト用のフレームワーク。
Railsのデフォルトのテスティングフレームワークでもある。
テストコードの書き方
1 2 3 4 5 6 7 8 |
require 'minitest/autorun' class SampleTest < Minitest::Test def test_sample assert_equal 'true' ,''.empty? assert_equal 'true' ,'aa'.empty? end end |
1行目テストライブラリの読み込み
3行目テストコードのクラスを定義
クラス名はキャメルケース、ファイル名はスネークケースで書く。
4〜7行目が実行対象となるテストメソッド
Minitestはtest_で始まるメソッドを探して実行するため、メソッド名はtest_で始めるようにする。
実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Run options: --seed 8358 # Running: F Finished in 0.001335s, 749.0636 runs/s, 1498.1273 assertions/s. 1) Failure: SampleTest#test_sample [test.rb:6]: Expected: true Actual: false 1 runs, 2 assertions, 1 failures, 0 errors, 0 skips |
最終行から1つ失敗したとわかる。
9行目から失敗した内容が記述されている。
期待値がtrueに対し、実際にはfalseであったと読み取れる。