Blame DOCUMENTATION

Packit 532126
Packit 532126
To use the lunit unit testing framework copy these files to your
Packit 532126
lua search path or include it to your package:
Packit 532126
Packit 532126
        lunit
Packit 532126
        lunit.lua
Packit 532126
        lunit-console.lua
Packit 532126
Packit 532126
Packit 532126
To write a testcase, open the framework using require. The "lunit" shell script
Packit 532126
works hard to find the "lunit.lua" and "lunit-console.lua" in all cases.
Packit 532126
Packit 532126
        require "lunit"
Packit 532126
Packit 532126
Packit 532126
Lunit uses the lua-5.1 module system. A testcase is a arbitrarily named module
Packit 532126
marked with "lunit.testcase" as a testcase. An example:
Packit 532126
Packit 532126
        require "lunit"
Packit 532126
Packit 532126
        module( "my_testcase", lunit.testcase, package.seeall )
Packit 532126
Packit 532126
Packit 532126
The tests itself in a testcase are functions whose names must begin
Packit 532126
or end with 'test'. The function names are case insensitive. Example:
Packit 532126
Packit 532126
        require "lunit"
Packit 532126
Packit 532126
        module( "my_testcase", lunit.testcase, package.seeall )
Packit 532126
Packit 532126
        function FirstTest()
Packit 532126
          -- Test code goes here
Packit 532126
        end
Packit 532126
Packit 532126
        function test_something()
Packit 532126
            -- Test code goes here
Packit 532126
        end
Packit 532126
Packit 532126
Packit 532126
Inside the test functions you use asserts to test your code or package.
Packit 532126
Lunit defines 26 assert functions:
Packit 532126
Packit 532126
        fail( [msg] )
Packit 532126
Packit 532126
              Always fails.
Packit 532126
Packit 532126
        assert( assertion, [msg] )
Packit 532126
Packit 532126
              Fails, if 'assertion' is false or nil.
Packit 532126
Packit 532126
        assert_true( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't true.
Packit 532126
Packit 532126
        assert_false( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't false. (Even fails if 'actual' is
Packit 532126
              a nil value!)
Packit 532126
Packit 532126
        assert_equal( expected, actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is different from 'expected'. Make sure
Packit 532126
              that you don't mix 'expected' and 'actual' because they are
Packit 532126
              used to build a nice error message.
Packit 532126
Packit 532126
        assert_not_equal( unexpected, actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' and 'unexpected' are equal.
Packit 532126
Packit 532126
        assert_match( pattern, actual, [msg] )
Packit 532126
Packit 532126
              Fails, if the string 'actual' doesn't match 'pattern'.
Packit 532126
Packit 532126
        assert_not_match( pattern, actual, [msg] )
Packit 532126
Packit 532126
              Fails, if the string 'actual' match 'pattern'.
Packit 532126
Packit 532126
        assert_nil( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a nil value.
Packit 532126
Packit 532126
        assert_not_nil( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a nil value.
Packit 532126
Packit 532126
        assert_boolean( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't true or false.
Packit 532126
Packit 532126
        assert_not_boolean( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is true or false.
Packit 532126
Packit 532126
        assert_number( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a number.
Packit 532126
Packit 532126
        assert_not_number( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a number.
Packit 532126
Packit 532126
        assert_string( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a string.
Packit 532126
Packit 532126
        assert_not_string( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a string.
Packit 532126
Packit 532126
        assert_table( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a table.
Packit 532126
Packit 532126
        assert_not_table( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a table.
Packit 532126
Packit 532126
        assert_function( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a function.
Packit 532126
Packit 532126
        assert_not_function( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a function.
Packit 532126
Packit 532126
        assert_thread( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't a thread (created by
Packit 532126
              coroutine.create or coroutine.wrap).
Packit 532126
Packit 532126
        assert_not_thread( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is a thread.
Packit 532126
Packit 532126
        assert_userdata( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' isn't userdata.
Packit 532126
Packit 532126
        assert_not_userdata( actual, [msg] )
Packit 532126
Packit 532126
              Fails, if 'actual' is userdata.
Packit 532126
Packit 532126
        assert_error( [msg], func )
Packit 532126
Packit 532126
              Fails, if 'func' doesn't raises an error (using error()).
Packit 532126
Packit 532126
        assert_pass( [msg], func )
Packit 532126
Packit 532126
              Fails, if 'func' raises an error.
Packit 532126
Packit 532126
Packit 532126
All assert functions take an optional message as the last argument. Only 
Packit 532126
assert_pass() and assert_error() require the optional message as the first
Packit 532126
argument. The last argument of these two are functions.
Packit 532126
Packit 532126
There are also useful functions to test for the type of a value:
Packit 532126
Packit 532126
        is_nil( actual )
Packit 532126
        is_boolean( actual )
Packit 532126
        is_number( actual )
Packit 532126
        is_string( actual )
Packit 532126
        is_table( actual )
Packit 532126
        is_function( actual )
Packit 532126
        is_thread( actual )
Packit 532126
        is_userdata( actual )
Packit 532126
Packit 532126
These all returns true if 'actual' is of correct type, otherwise false. 
Packit 532126
Packit 532126
You use the assert functions and the is_type functions in your tests to check
Packit 532126
your code or package. Example:
Packit 532126
Packit 532126
        require "lunit"
Packit 532126
Packit 532126
        module( "my_testcase", lunit.testcase, package.seeall )
Packit 532126
Packit 532126
        function FirstTest()
Packit 532126
          local result = compute_some_value()
Packit 532126
          assert_string( result )
Packit 532126
          assert_equal("foobar", result)
Packit 532126
        end
Packit 532126
Packit 532126
        function test_something()
Packit 532126
          local result = flip_coin()	-- flip_coin returns at random 0 or 1
Packit 532126
          assert_number(result)
Packit 532126
          if result == 0 then
Packit 532126
            -- ok
Packit 532126
          elseif result == 1 then
Packit 532126
            -- ok
Packit 532126
          else
Packit 532126
            fail("flip_coin: invalid number: "..tostring(result))
Packit 532126
          end
Packit 532126
        end
Packit 532126
Packit 532126
Packit 532126
You can define the functions setup() and teardown() if you have to allocate
Packit 532126
some resources or obtain some handles for your tests.
Packit 532126
The setup() function is called before every test and teardown() is
Packit 532126
called after every test. Example:
Packit 532126
Packit 532126
        require "lunit"
Packit 532126
Packit 532126
        module( "resource_testcase", lunit.testcase, package.seeall )
Packit 532126
Packit 532126
        local orig_content, handle
Packit 532126
Packit 532126
        function setup()
Packit 532126
          orig_content = { "row 1", "row 2", "row 3" }
Packit 532126
          handle = database_open("test.db")
Packit 532126
          database_create_table(handle, ...)
Packit 532126
          database_fill_table(handle, orig_content, ...)
Packit 532126
        end
Packit 532126
Packit 532126
        function teardown()
Packit 532126
          database_drop_table(handle, ...)
Packit 532126
          database_close(handle)
Packit 532126
          handle = nil
Packit 532126
          orig_content = nil
Packit 532126
          delete_file("test.db")
Packit 532126
        end
Packit 532126
Packit 532126
        function test_select()
Packit 532126
          local content = database_select(handle, ...)
Packit 532126
          assert_table( content )
Packit 532126
          assert_equal( orig_content, content )
Packit 532126
        end
Packit 532126
Packit 532126
        function test_insert()
Packit 532126
          database_insert(handle, "row 4", ...)
Packit 532126
          local content = database_select(handle, ...)
Packit 532126
          assert_table( content )
Packit 532126
          assert_equal( { "row 1", "row 2", "row 3", "row 4" }, content )
Packit 532126
        end
Packit 532126
Packit 532126
        function test_delete()
Packit 532126
          database_delete(handle, "row 2", ...)
Packit 532126
          local content = database_select(handle, ...)
Packit 532126
          assert_table( content )
Packit 532126
          assert_equal( { "row 1", "row 3" }, content )
Packit 532126
        end
Packit 532126
Packit 532126
Packit 532126
To run your testcases, simply use the shell script "lunit". Example:
Packit 532126
Packit 532126
        # ./lunit my_testcase.lua
Packit 532126