Monday, December 18, 2006

[temp] pages 147-8


Listen to this article.

Summary:

You can test exceptions, too (see example code 2).

The final parameter to every assertion is a message, which is output before any
failure message. This normally isn't needed, as Test::Unit's messages are normally
pretty reasonable. The one exception is the test assert_not_nil, where the message
"<nil> expected to not be nil" doesn't help much. In that case, you may want to add
some annotation of your own (see example code 3).

 

Example:

require 'roman'
require 'test/unit'
class TestRoman < Test::Unit::TestCase
NUMBERS = [
[ 1, "i" ], [ 2, "ii" ], [ 3, "iii" ],
[ 4, "iv"], [ 5, "v" ], [ 9, "ix" ]
]
def test_simple
NUMBERS.each do |arabic, roman|
r = Roman.new(arabic)
assert_equal(roman, r.to_s)
end
end
end

require 'roman'
require 'test/unit'
class TestRoman < Test::Unit::TestCase
def test_range
assert_raise(RuntimeError) { Roman.new(0) }
assert_nothing_raised() { Roman.new(1) }
assert_nothing_raised() { Roman.new(499) }
assert_raise(RuntimeError) { Roman.new(5000) }
end
end

require 'test/unit'
class TestsWhichFail < Test::Unit::TestCase
def test_reading
assert_not_nil(ARGF.read, "Read next line of input")
end
end

produces:

Loaded suite -
Started
F
Finished in 0.033581 seconds.
1) Failure:
Read next line of input.
<nil> expected to not be nil.
1 tests, 1 assertions, 1 failures, 0 errors
test_reading(TestsWhichFail) [ prog.rb:4]:

Reported errata: 1
#1944 minor typo
 

0 Comments:

Post a Comment

<< Home