Ruby - Unit Testing Unit Testing

Introduction

Ruby has with a library, Test::Unit, that makes testing easy.

You can organize test cases into a clean structure.

Unit testing is the primary component of test-driven development.

Test::Unit is Ruby's official library for performing unit tests.

Test::Unit gives you a core set of assertions to use.

Demo

class String 
  def titleCase #  w  ww. j ava 2  s  .  c om
    self.gsub(/(\A|\s)\w/){ |letter| letter.upcase } 
  end 
end 

require 'test/unit' 

class TestTitleize < Test::Unit::TestCase 
  def test_basic 
    assert_equal("This Is A Test", "this is a test".titleCase) 
    assert_equal("Another Test 1234", "another test 1234".titleCase) 
    assert_equal("We're Testing", "We're testing".titleCase) 
  end 
end

Result

Here, you include the titleCase extension to String.

Next you load the Test::Unit class using require.

Finally you create a test case by inheriting from Test::Unit::TestCase.

Related Topics