Io/IoUnit

edit
revisions

what's new
search
help

kiwi


Introduction

IoUnit is a simple unit-testing framework designed to minimize the effort required for Io programmers to implement automated testing for their modules and applications. Many of IoUnit's concepts were borrowed directly from SUnit? of Smalltalk fame, although they have been adapted to fit better with the design of the Io language. Programmers who are interested in SUnit? and have minimal familiarity with Smalltalk should refer to http://sunit.sourceforge.net for more information about unit testing.

An Example of Use

 doFile( "IoUnit.io" )

 IoUnit suite( 
     setUp( 
         foo = "foo"
         alsoFoo = "foo"
         bar = "bar"
         foobar = "foobar" 
     )
     case( "A string should be equivalent to itself.",
         should( foo == foo )        
         shouldnt( foo != foo )
     )
     case( "A string should be equivalent to another string containing the same characters.",
         should( foo == alsoFoo )
     )   
     case( "String 'foo' is not equivalent to String 'bar'",
         should ( foo != bar )
         shouldnt( foo == bar )
     )
     case( "String 'foo' + String 'bar' should be equivalent to String 'foobar'.",
          should( foo + bar  == foobar)
     )
 ) run report

Explanation of the Example

This example defines a very brief test suite for testing one of the comparison operators and its relationship with the String primitive in Io.

The suite method of IoUnit creates an instance of IoUnit TestSuite?, which encloses three important facets of testing: the setup, teardown and actual test cases. It accepts a message chain as its sole argument, which should be used to define the tests that the test suite should perform. When a TestSuite?'s run method is invoked, it will automatically create a TestRun? instance, supply it with a setup and teardown message chain, and furnish it with a list of defined test cases to evaluate, then execute the test run. The TestRun? object will be returned at the end of the test pass, and will contain a list of passed and failed test cases, and may be used to determine the relative success of the test.

The setUp and tearDown methods of a TestSuite? are used to declare a message chain that will be evaluated in the context of subsequent test runs before and after the test cases are evaluated, respectively. The case method accepts two arguments, a string summarizing the test case and a message chain that, when evaluated, will exercise a fixture of the module or application being tested. Each case may make use of the should and shouldnt constructs to make assertions about various facets of the test case. A should message accepts an expression that should return non-Nil if the test has passed, and the shouldnt message accepts an expression that should return Nil if the test has passed. If the case reaches the end of its message chain without failing a should or shouldnt test, and without raising any exceptions, it is added to the test run's list of passed tests. Otherwise, information about the failed test case is added to the run's failed test case list.

Information about a test run can be displayed to the standard output device by using TestRun?'s report method

--written, June 24, 2003, by Scott Dunlop.....

last modified on September 27, 2005, at 03:35 PM

© type-z.org and its contributing authors, 2001-2004.
Content is licensed under a Creative Commons License.