添付ライブラリ: 開発ツール

Test Unit

普段から使っているので特に目新しいものはないが、flunkassert_raiseだけは普段使っていないので、参考になった。

assert_errorはクラスを指定せず、raiseだけさせておくとRuntime errorになってしまうので、Standard Errorであることを明示しておく必要がある。なお継承関係は RuntimeError < StandardError になっていて、指定のないエラーだけがRuntime Errorとなる。

また、test_instance_ofに指定できるのがインスタンスのクラスだけであるのに対して、kind_ofはクラスの継承関係にあるものであれば、なんでも指定できる。

require 'test/unit'
require './foo'

class TestFoo < Test::Unit::TestCase
  def setup
    @foo = Foo.new
  end

  def teardown
    puts '  ==>  test compleated'
  end

  def test_foo
    assert_equal('foo', @foo.foo)
  end

  def test_hello
    assert_equal('Hello World', @foo.hello('World'))
  end

  def test_instance_of
    assert_instance_of(Foo,@foo)
  end

  def test_kind_of
    assert_kind_of(Object, @foo)
  end

  def test_match
    assert_match(/wor.{2}/,@foo.hello('world'))
  end

  def test_assert_raise
   return assert_raise(StandardError){@foo.error}
  end

  def test_flunk
    return flunk('still on pregress')
  end

end
pyons@LAPTOP-SF87NLCB:/mnt/c/Users/broad/OneDrive/products/Ruby認定技術者試験Gold$ ruby test_foo.rb
Run options: 

# Running tests:

[1/7] TestFoo#test_assert_raise  ==>  test compleated
[2/7] TestFoo#test_flunk  ==>  test compleated
 = 0.00 s
  1) Failure:
TestFoo#test_flunk [test_foo.rb:38]:
still on pregress

[3/7] TestFoo#test_foo  ==>  test compleated
[4/7] TestFoo#test_hello  ==>  test compleated
[5/7] TestFoo#test_instance_of  ==>  test compleated
[6/7] TestFoo#test_kind_of  ==>  test compleated
[7/7] TestFoo#test_match  ==>  test compleated
Finished tests in 0.048328s, 144.8451 tests/s, 165.5372 assertions/s.
7 tests, 8 assertions, 1 failures, 0 errors, 0 skips

ruby -v: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]

RDoc