LuaUnit

From Freehackers
Jump to: navigation, search

Unit Testing for Lua

by Bluebird

Contents

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.

Yzis uses LuaUnit a lot for its internal testing: see the test direectory.

Download

LuaUnit is developed inside the Lua Forge.

You can download the different versions here.

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 LGPL v2.

Feedback

LuaUnit is developed by Bluebird

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

Other Projects of Bluebird

  • PyTiCroque: a software to help playing at www.croquemonster.com
  • Indent Finder: set your editor to the correct indentation automatically
  • Klotski: a mind-breaking game
  • VimWrapper: a library to integrate Vim into other software

Personal tools
Namespaces
Variants
Actions
fh network
Freehackers
Navigation
Toolbox