LuaUnit

From Freehackers

Jump to: navigation, search

Unit Testing for Lua

by Bluebird

Contents

[edit] 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.

[edit] 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:

  • Tests aggregation into test suites.
  • New test classes (anything that begins with "Test") are automatically detected and added to the test suite
  • Test functions based on the use of "assert" are easily integrated
  • File, line and detailed failure reason are displayed when something goes wrong
  • Classical setUp/tearDown method for setting up a tearing down the test environment
  • Output can be easily customized to your needs (need xml output ?)
  • Report the percentage success and number of failure and successes

[edit] 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.

[edit] Download

LuaUnit is developed inside the Lua Forge.

You can download the different versions here.

[edit] 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.

[edit] Feedback

LuaUnit is developed by Bluebird

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

[edit] Other Projects of Bluebird

  • VimWrapper: a library to integrate Vim into other software
  • Vy: a generic vi engine written in Python
  • LuaUnit: a unittest framework for the Lua language
  • Klotski: a mind-breaking game
  • Yzis: a generic vi engine written in C++
  • Indent Finder: set your editor to the correct indentation automatically
  • Pretty Make: beautify your make output
  • Keep Cool: keep your CPU at low temperature while compiling.
  • KMerge: text document merging tool developed for KDE 1.

Personal tools