Thursday, May 5, 2011

GUIDE: LUA unit testing and TDD

I've just uncovered this subject, here is my insights:

Fast start
  1. Open LUA
  2. Start a new test file (let's call it 'my_code.tests.lua').
  3. Edit 'my_code.tests.lua' (example template below)


--- my_code.tests.lua Code --------------------------------------------

-- Some super function to test
function my_super_function( arg1, arg2 ) return arg1 + arg2 end

-- Unit testing starts
require('luaunit')

TestMyStuff = {} --class
    function TestMyStuff:testWithNumbers()
        a = 1
        b = 2
        result = my_super_function( a, b )
        assertEquals( type(result), 'number' )
        assertEquals( result, 3 )
    end

    function TestMyStuff:testWithRealNumbers()
        a = 1.1
        b = 2.2
        result = my_super_function( a, b )
        assertEquals( type(result), 'number' )
        -- I would like the result to be always rounded to an integer
        -- but it won't work with my simple implementation
        -- thus, the test will fail
        assertEquals( result, 3 )
    end

-- class TestMyStuff

luaUnit:run()


--- Code End -----------------------------------------------------------------------
  1. Execute the test code.
  2. Add your tests and assumption checking.
  3. Fix what's broken.
Understand more

No comments:

Post a Comment