section9/unittest.rb README
Release: 0.0.1
Overview
section9/unittest.rb is an extension library for Test::Unit, supporting both 1.8 and 1.9.
- describe() and it() are available. ** before(), after(), subject() are NOT supported.
- verify_() helps to write assertion easily.
Examples:
Example:
require 'test/unit'
require 'section9/unittest'
class ExampleTest < TC # or Section9::UnitTest::TestCase
describe "Array" do
describe "#collect()" do
it "collects values for each item" do
actual = [10, 20, 30].collect {|x| x + 1}
verify_(actual) == [11, 21, 31]
## or assert_xxx() are available
assert_equal([11, 21, 31], actual)
end
end
end
end
Assertions:
verify_(actual) == expected # same as assert_equal(expected, actual)
verify_(actual) != expected # same as assert_not_equal(expected, actual) # for Ruby1.9
verify_(actual) > expected # same as assert_operator(actual, :>, expected)
verify_(actual) >= expected # same as assert_operator(actual, :>=, expected)
verify_(actual) < expected # same as assert_operator(actual, :<, expected)
verify_(actual) <= expected # same as assert_operator(actual, :>= expected)
verify_(actual) =~ expected # same as assert_match(expected, actual)
verify_(actual) !~ expected # same as assert_no_match(expected, actual) # for Ruby 1.9
verify_(act).in_delta?(exp, delta) # same as assert_in_delta(exp, act, delta)
verify_(actual).nil? # same as assert_nil(actual)
verify_(actual).same?(expected) # same as assert_same(expected, actual)
verify_(actual).kind_of?(klass) # same as assert_kind_of(klass, actual)
verify_(actual).instance_of?(klass) # same as assert_instance_of(klass, actual)
verify_(proc {...}).raise?(klass, errmsg)
# same as ex = assert_raise(klass) {...};
# assert_equal(errmsg, ex.msg) if errmsg.is_a?(String)
# assert_match(errmsg, ex.msg) if errmsg.is_a?(Regexp)
Any boolean method can be assertion:
verify_(actual).empty? # same as assert actual.empty? == true
verify_(actual).blank? # same as assert actual.blank? == true
verify_(actual).end_with?(arg) # same as assert actual.end_with?(arg) == true
Negative assertion:
## you should use '.NOT ==' and '.NOT =~instead of '!=' and '!~' on Ruby 1.8,
## because 1.8 doesn't allow to override '!=' nor '!~' operators.
verify_(1+1).NOT == 3 # same as assert_not_equal(3, 1+1)
verify_("SOS").NOT =~ /\d+/ # same as assert_no_match(/\d+/, "SOS")
verify_(proc {...}).NOT.raise?
# same as assert_nothing_raised {...}
Original assertion:
verify_(path).file_exist? # same as assert File.file?(path)
verify_(path).dir_exist? # same as assert File.dir?(path)
verify_(path).NOT.exist? # same as assert ! File.exist?(path)
verify_(recorder).called?(...) # see the next section
Helper method 'at_exit()' registers block which is called in teardown() method:
class Example_TC < TC
it "is an example to remove temporary file automatically" do
File.open("tmp.txt", 'w') {|f| f.write("SOS") }
at_exit { File.unlink("tmp.txt") } # register block
verify_(File.read("tmp.txt")) == "SOS"
end
end
Helper method 'tmp()' returns Section::Tmp object (see the next section):
class Example_TC < TC
it "is an example to capture $stdout" do
## create new StringIO object and set it to $stdout
sout = tmp.stdout()
## print something
puts "SOS"
## do test
verify_(sout.string) == "SOS\n"
## $stdout is recovered automatically in teardown() method.
end
end
Helper method 'recorder()' returns Section::Recorder object (see the next section):
class Calc
def total(*nums)
return nums.sum
end
def average(*nums)
return total(nums).to_f / nums.length
end
end
class Example_TC < TC
it "is an example to record method calls" do
rc = recorder() # create recorder object
calc = Calc()
rc.record(calc, :total, :average) # register methods to record
n = calc.average(10, 20, 9)
verify_(n) == 13
## check method call
verify_(rc[0]).called?(:obj=>calc, :name=>:average, :args=>[10, 20, 9], :ret=>13)
verify_(rc[1]).called?(:obj=>calc, :name=>:total, :args=>[10, 20, 9], :ret=>39)
## or
verify_(rc[0]).called?([calc, :average, [10, 20, 9], 13])
verify_(rc[1]).called?([calc, :total, [10, 20, 9], 39])
## or
verify_(rc[0].obj) == calc
verify_(rc[0].name) == :average
verify_(rc[0].args) == [10, 20, 9]
verify_(rc[0].ret) == 13
end
end
These helper methods are defined in TC (== Section9::UnitTest::TestCase) class, therefore you can use them in 'test_xxx()' methods:
class Example_TC < TC
def test_1
verify_(1+1) == 2
at_exit { ... }
sout = tmp.stdout()
rc = record()
end
end
History
- 0.1.0 ** public release
License
$License: MIT License $
Copyright
$Copyright: copyright(c) 2011 kuwata-lab.com all rights reserved $