CSC/ECE 517 Fall 2010/ch1 1f TU: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
= Unit-testing frameworks for Ruby = x
= Unit-testing frameworks for Ruby =  


== Unit Testing ==
== Unit Testing ==

Revision as of 01:45, 5 September 2010

Unit-testing frameworks for Ruby

Unit Testing

A unit is the smallest building block of a software. Such a unit can be: a class, a method, an interface etc. Unit testing is the process of validating such units of code.

Benefits

Some of the benefits are:

  • Proof of your code
  • Better design - Thinking about the tests can help us to create small design elements, thereby improving the modularity and reusability of units.
  • Safety net on bugs - Unit tests will confirm that while refactoring no additional errors were introduced.
  • Be able to detect and remove defects in a more cost effective manner compared to the other stages of testing.
  • Be able to test parts of a source code in isolation.
  • Making debugging more efficient by searching for bugs in the probable code areas.
  • Documentation - Designers can look at the unit test for a particular method and learn about its functionality.

Unit-testing frameworks

Unit Test Framework is a software tool to support writing and running unit test.

List of unit testing frameworks for Ruby

  • Test::Unit
  • RSpec
  • Shoulda
  • Cucumber

Simple Calculator Program in Ruby

class Calculator 
 
 attr_writer :number1 
 attr_writer :number2
 
 def initialize(number1,number2)
   @number1 = number1
   @number2 = number2
 end
#-----------Addition of two numbers----------------#
 def addition
   result = @number1 + @number2
   return result
 end
#----------Subtraction of two numbers--------------#
  def subtraction
    result= @number1 - @number2
    return result
  end
#----------Multiplication of two numbers------------#
  def multiplication
    result= @number1 * @number2
    return result
  end
#-----------Division of two numbers-------------------#
 def division
   result = @number1 / @number2
   return result
 end
end

Test::Unit

require "calculator"
require "test/unit"
class TC_Calculator < Test::Unit::TestCase
 
 #---------------Testing method Addition-------------#
 def test_addition
   assert_equal(8,Calculator.new(3,4).addition)
  
 end
#----------------Testing method Subtraction----------#
 def test_subtraction
  assert_same(1,Calculator.new(4,3).subtraction)
 end
#----------------Testing method Multiplication-------#
 def test_multiplication
  assert_not_same(12,Calculator.new(3,4).multiplication)
 end
#----------------Testing method Division-------------#
 def test_division
    assert_not_equal(5,Calculator.new(8,2).division)
 end
end