Cuando una assertion falla

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ cat test/tc_simple_number2.rb 
# File:  tc_simple_number2.rb
 
require "simple_number"
require "test/unit"
 
class TestSimpleNumber < Test::Unit::TestCase
 
  def test_simple
    assert_equal(4, SimpleNumber.new(2).add(2) )
    assert_equal(4, SimpleNumber.new(2).multiply(2) )
  end
 
  def test_typecheck
    assert_raise( RuntimeError ) { SimpleNumber.new('a') }
  end
 
  def test_failure
    assert_equal(3, SimpleNumber.new(2).add(2), "Adding doesn't work" )
  end
 
end

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ cat Rakefile 
desc "Test class SimpleNumber"
task :test do
  sh "ruby -Ilib -Itest test/tc_simple_number.rb"
end

desc "Test with test/tc_simple_number2.rb. Expect failures"
task :test2 do
  sh "ruby -Ilib -Itest test/tc_simple_number2.rb"
end

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ rake test2
ruby -Ilib -Itest test/tc_simple_number2.rb
Run options: 

# Running tests:

F..

Finished tests in 0.001706s, 1758.4994 tests/s, 2344.6659 assertions/s.

  1) Failure:
test_failure(TestSimpleNumber) [test/tc_simple_number2.rb:18]:
Adding doesn't work.
<3> expected but was
<4>.

3 tests, 4 assertions, 1 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -Ilib -Itest test/tc_simple_number2.r...]
/Users/casiano/local/src/ruby/LPP/unit_testing/simple_example/Rakefile:8:in `block in <top (required)>'
/Users/casiano/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
/Users/casiano/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test2
(See full trace by running task with --trace)
  1. Now there are three tests (three member functions) in the class.

  2. The function test_typecheck uses assert_raise to check for an exception.

  3. The function test_failure is set up to fail, which the Ruby output happily points out, not only telling us which test failed, but how it failed (expected <3> but was <4>).

  4. On this assertion, we've also added an final parameters which is a custom error message. It's strictly optional but can be helpful for debugging.

  5. All of the assertions include their own error messages which are usually sufficient for simple debugging.

Casiano Rodriguez León 2015-01-07