CSC/ECE 517 Fall 2010/ch1 1f vn

From Expertiza_Wiki
Revision as of 20:58, 6 September 2010 by Desmond (talk | contribs) (→‎Assertions)
Jump to navigation Jump to search

Introduction

Unit testing is a method by which we can isolate and test a unit functionality of the program, typically individual methods during and long after the code is written. Unit testing frameworks provides us with constructs and methodologies which simplifies the process of unit testing. This chapter walks through three different unit testing frameworks available for Ruby, explains how to use them with examples, and compares them with one another. The three commonly used unit testing frameworks for ruby are

  1. Test::Unit
  2. Shoulda
  3. RSpec


Test::Unit

Ruby comes with an in-built, ready to use unit testing framework called Test::Unit. It is a XUnit type framework and typically have a setup method for initialization, a teardown method for cleanup and the actual test methods itself. The tests themselves are bundled separately from the code it is testing.

Test Fixture

Test fixture represents the initial environment setup(eg. initialization data) and the expected outcome of the tests for that environment. This is typically done in the setup()/teardown() methods and it helps to separate test initialization/cleanup from the actual tests. It also helps to reuse the same fixture for more than one tests.

Assertions

The core part of test::unit framework is the ability to assert a statement of expected outcome. If an assert statement is correct then the test will proceed, otherwise the test will fail. Test::unit provides a bunch of assert methods for this purpose. | border=1 cellspacing=0 cellpadding=5 | assert( boolean, [message] ) | True if boolean |- | assert_equal( expected, actual, [message] )
assert_not_equal( expected, actual, [message] ) | True if expected == actual |- | assert_match( pattern, string, [message] )
assert_no_match( pattern, string, [message] ) | True if string =~ pattern |- | assert_nil( object, [message] )
assert_not_nil( object, [message] ) | True if object == nil |- | assert_in_delta( expected_float, actual_float, delta, [message] ) | True if (actual_float - expected_float).abs <= delta |- |assert_instance_of( class, object, [message] ) | True if object.class == class |- |assert_kind_of( class, object, [message] ) | True if object.kind_of?(class) |- | assert_same( expected, actual, [message])
assert_not_same( expected, actual, [message] ) | True if actual.equal?( expected ). |- | assert_raise( Exception,... ) {block}
assert_nothing_raised( Exception,...) {block} | True if the block raises (or doesn't) one of the listed exceptions. |- | assert_throws( expected_symbol, [message] ) {block}
assert_nothing_thrown( [message] ) {block} | True if the block throws (or doesn't) the expected_symbol. |- | assert_respond_to( object, method, [message] ) | True if the object can respond to the given method. |- | assert_send( send_array, [message] ) | True if the method sent to the object with the given arguments return true. |- | assert_operator( object1, operator, object2, [message] ) | Compares the two objects with the given operator, passes if true |- |}


For the full list of assertion methods provided by test::unit refer to test::unit assertions

Example

The test methods should start with 'test' prefix. This helps in isolating the test methods from the helper methods.

Test Suite

Shoulda

RSpec

Comparison