3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

assert_equalを作って簡単にテストを書けるようにする

Last updated at Posted at 2020-12-29

!macOS-11.1 !ruby-2.7.2p137

Preface (はじめに)

テスト駆動型開発(TDD)では簡易的なテストを何度も繰り返し開発を進めることが基本です.

本記事では, ある式や値が想定のものと同じかどうかを確かめる関数assert_equalを作り, それを用いて簡単なテストコードを作ろうと思います.

Assert

Equal

まずはexpectedresultの値が等しいか確かめる

assert_equal.rb
def assert_equal(expected, result)
  return expected == result
end

p assert_equal(1, 1)
> ruby assert_equal.rb
-> true

次に視覚的に分かりやすくするためcolorizeライブラリを用いて出力に色をつけます.

assert_equal.rb
require 'colorize'

def assert_equal(expected, result)
  if expected == result
    puts 'true'.green
  else
    puts 'false'.red
  end
end

assert_equal(1, 1)
assert_equal(1, 2)

結果は一つ目は緑のtrue, 二つ目は赤のfalseとなるはずです.

少し改良を加えます.

  • 引数のexpectedとresultの値を出力
  • 先ほどのassert_equalの処理を以下のように変更
    • true: print "succeeded in assert_equal.\n".green
    • false: print "failed in assert_equal.\n".red
assert_equal.rb
require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end
def assert_equal(expected, result)
  puts_vals(expected, result)
  print  case expected == result
     when true  ; "succeeded in #{__method__}.\n".green
     when false ; "failed in #{__method__}.\n".red
     end
end

assert_equal(1, 1)
assert_equal(1, 2)

出力は

> ruby assert_equal.rb
expected :: 1
result   :: 1
succeeded in assert_equal.
expected :: 1
result   :: 2
failed in assert_equal.

となります. (Qiita上では色は変わっていません.)

Not Equal

今度はexpectedresultが等しければfalse, 異なればtrueを返すassert_not_equal関数を作ります.

assert_equal.rb
require 'colorize'

def puts_vals(expected, result)
  puts "expected :: #{expected}"
  puts "result   :: #{result}"
end
def assert_not_equal(expected, result)
  puts_vals(expected, result)
  print expected != result ?
  "succeeded in #{__method__}.\n".green :
    "failed in #{__method__}.\n".red
end
def assert_equal(expected, result)
  puts_vals(expected, result)
  print  case expected == result
     when true  ; "succeeded in #{__method__}.\n".green
     when false ; "failed in #{__method__}.\n".red
     end
end

assert_equal(1, 1)
assert_equal(1, 2)
assert_not_equal(1, 2)
assert_not_equal(1, 1)

出力は

> ruby assert_equal.rb
expected :: 1
result   :: 1
succeeded in assert_equal.
expected :: 1
result   :: 2
failed in assert_equal.
expected :: 1
result   :: 2
succeeded in assert_not_equal.
expected :: 1
result   :: 1
failed in assert_not_equal.

このassert_equalやassert_not_equalは便利なので他のコードにも使いたいです. しかしこのままでは, コード内のテストも実行されてしまうので,

assert_equal.rb
if $PROGRAM_NAME == __FILE__
  assert_equal(1, 1)
  assert_equal(1, 2)
  assert_not_equal(1, 2)
  assert_not_equal(1, 1)
end

というようにテスト部分を変えます.
このように書くことでcodeが書いてあるファイル名(_FILE_)とPROGRAMが動いているファイル名($PROGRAM_FILE)とが一致したら, 中身を実行するようにできます.

参考資料

チャート式ruby-IV(assert_equal)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?