Ejemplo Sencillo de uso de test/unit

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ tree
.
|-- Rakefile
|-- lib
|   `--- simple_number.rb
`--- test
    `--- tc_simple_number.rb

2 directories, 3 files

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ cat lib/simple_number.rb 
# File:  simple_number.rb
 
class SimpleNumber
 
  def initialize(num)
    raise unless num.is_a?(Numeric)
    @x = num
  end
 
  def add(y)
    @x + y
  end
 
  def multiply(y)
    @x * y
  end
 
end

Véase la documentación en Test/Unit

[~/local/src/ruby/LPP/unit_testing/simple_example(master)]$ cat test/tc_simple_number.rb 
# File:  tc_simple_number.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(6, SimpleNumber.new(2).multiply(3) )
  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

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

# Running tests:

.

Finished tests in 0.000530s, 1886.7925 tests/s, 3773.5849 assertions/s.

1 tests, 2 assertions, 0 failures, 0 errors, 0 skips

Failures are when your test cases fail - i.e. your assertions are incorrect. Errors are unexpected errors that occur while trying to actually run the test - exceptions, etc.



Casiano Rodriguez León 2015-01-07