Luaunit

Unit testing for Lua


by Philippe Fremy

Introduction

Unit testing is a basic requirement of any big development that want to be stable and guarantee non-regression. Lua was missing a good unit testing framework at the moment where I wrote this, so I filled up the gap.



Description

luaunit is modeled after python unit testing framework, which I have found the best testing framework so far. What I enjoy the most is the use of the reflection capabilities of the language to automatically build test suites.

LuaUnit is based on the initial work of Ryu Gwang.

Luaunit features:



Documentation

The documentation is quite limited but should suffice to anybody familiar with programming and testing.

Create a new test class
TestToto = {} --class

    function TestToto:setUp()
        -- set up tests
		self.a = 1
		self.s = 'hop' 
    end

    function TestToto:test1()
		print( "some stuff test 1" )
        assertEquals( self.a , 1 )
        assertEquals( self.a , 2 )
        assertEquals( self.a , 2 )
    end

    function TestToto:test2()
		print( "some stuff test 2" )
        assertEquals( self.a , 1 )
        assertEquals( self.s , 'hop' )
        assertEquals( self.s , 'bof' )
        assertEquals( self.s , 'bof' )
    end

    function TestToto:test3()
		print( "some stuff test 3" )
        assertEquals( self.a , 1 )
        assertEquals( self.s , 'hop' )
        assertEquals( type(self.a), 'number' )
    end
-- class TestToto

Run the test class
philippe@werewindle ~/work/lua $ lua use_luaunit.lua TestToto
>>>>>> TestToto
>>> TestToto:test3
some stuff test 3
Ok
>>> TestToto:test1
some stuff test 1
use_luaunit.lua:15: expected: 1, actual: 2
Failed
>>> TestToto:test2
some stuff test 2
use_luaunit.lua:23: expected: 'hop', actual: 'bof'
Failed

Success : 33% - 1 / 3

If you want more documentation, just look at the file use_luaunit.lua . It speaks by itself.

You might as well have a look on the lua wiki.

Download

You can find the latest version of luaunit and the sources on LuaForge

License

The original license is unclear but I guess it is public domain. Since this is derived work, it is even less clear. Anyway, I give it to you under whatever license you wish.

Feedback

LuaUnit was written and is used extensively in Yzis (a vi clone).

Bugs, suggestions, patch, I am open to feedback: Mail me.





Last modification : $Date: 2005/07/02 16:19:31 $ - $Author: philippe $