Unit test
Comprised of two classes :
- Test.lua
-
The test runner. Once the tests are written, this class
is used to run them and to collect the results. It can be
run from the shell :
lua 'PATH_TO_btd/lua/test.lua'/test.lua SWITCHES PARAMETERS
or from a script :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit.errorIDE = true -- SWITCHES
LuaUnit:run(PARAMETERS)
- - SWITCHES
-
- - errorIDE
-
Turn IDE integration error format on (default =
off). In this mode the summary of failed tests is
send to 'stderr' instead of 'stdout' and the
output format is changed to :
Failed:PATH_TO_TESTPROGRAM/TESTPROGRAM|XX| TESTMETHOD
with 'XX' = line number. From the shell it is
turned on by '-e', and from a script as follows :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit.errorIDE = true
see also jEdit integration
- - verbosity
-
Turn verbosity on (default = off when run from
the shell and on when run from a script). From the
shell it is turned on by '-v', and from a script
it can be turned off as follows :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit.verbosity = 0 -- default = 1
- - help
-
Display a help message, only makes sense when
run from the shell. Displayed with '-help', '-h'
or '-?'.
- - PARAMETERS
-
- - no parameters
-
Test.lua
will scan the global environment (_G)
for functions who's name start with Test
. Each
of these functions will then be scanned for
methods who's name start with test
. All found
methods will be called one by one. This can only
be called from a script :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit:run()
- - comma separated list of class names
-
Test.lua
will scan each of these classes for
methods who's name start with 'test'
. All found
methods will be called one by one. This is
called from the shell as follows :
lua 'PATH_TO_btd/lua/test.lua'/test.lua SWITCHES Test1 Test2
or from a script :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit:run('Test1','Test2')
- - comma separated list of method names
-
Test.lua
will call each of these methods one by
one. This is called from the shell as follows :
lua 'PATH_TO_btd/lua/test.lua'/test.lua SWITCHES Test1:test1 Test2:test2
or from a script :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit:run('Test1:test1','Test2:test2')
- - remarks
-
-
It is possible to run a single class or method
with
LuaUnit:run('Test1:test4')
-
Function- and methodnames may be mixed as in
LuaUnit:run('Test1','Test2:test1')
- Tests are sorted alphabetically (first by class, then by method)
-
The mentioned naming conventions are not
mandatory, but testmethods will only be found
automatically if they are followed. Thus :
-
LuaUnit:run('function')
will scan class
function
for testXXX
methods
-
LuaUnit:run('function:method')
will run
the function:method()
test
-
when giving command line SWITCHES these are to
come before the PARAMETERS
- command line switches '-v' and '-e' may be combined
There are a number of built in helper functions :
- - test runner methods
-
- - setUpClass (optional)
-
Any class level initialisation should go here,
it is only executed once for each test class.
- - tearDownClass (optional)
-
Any class level clean up should go here, it is
only executed once for each different test class.
- - setUp (optional)
-
Any method level initialisation should go here,
it is executed before each individual test method.
- - tearDown (optional)
-
Any method level clean up should go here, it is
executed after each individual test method.
Order of execution example :
local LuaUnit = require('btd.lua.test.lua)()
...
LuaUnit:run('Test1:test1','Test1:test2','Test3')
Will run :
Test1:setUpClass()
Test1:setUp()
Test1:test1()
Test1:tearDown()
Test1:tearDownClass()
Test1:setUpClass()
Test1:setUp()
Test1:test2()
Test1:tearDown()
Test1:tearDownClass()
Test3:setUpClass()
Test3:setUp()
Test3:test1()
Test3:tearDown()
Test3:setUp()
Test3:test2()
Test3:tearDown()
- ...
Test1:tearDownClass()
- - test helper methods
-
- - clear
-
Clear the test statistics. While in one session
(of the stand alone interpreter for example) test
statitics are accumulated.
- - clearAll
-
Clear all
TestXXX
classes from the global
environment. Of minor importance now
because Test.lua
will do it's own clean up
(except of course if there is for some reason
a TestXX
class in the global environment).
- TestApi.lua
-
The methods used to write test classes.
- - fails
-
Test whether a function throws an (expected) exception.
local test = require('btd.lua.testapi.lua)
...
test.fails(error('error'))
For LuaUnit compatibility also known under the synonyms
assertError
and assert_Error
.
-- no 'TestAp' require statement necesarrylocal LuaUnit = require('btd.lua.test.lua)()
...
assertError(error('error'))
- - equals
-
Test wheter a function gives the expected result.
local test = require('btd.lua.testapi.lua)
...
test.equals(1 + 1,2)
For LuaUnit compatibility also known under the synonyms
assertEquals
and assert_Equals
.
-- no 'TestAp' require statement necesarrylocal LuaUnit = require('btd.lua.test.lua)()
...
assertEquals(1 + 1,2)
Can also analyse table results, with following remarks :
- tables are equal if :
- Both tables have the same length
- At the same key find the same value
-
The check is recursive, so tables within tables
will also be analysed
- - wrap
-
Wrap a number of test functions in a test class.
local test = require('btd.lua.testapi.lua)
...
TestFunction = test.wrap('test1','test2','test3'
For LuaUnit compatibility also known under the synonym
wrapFunctions
.
-- no 'TestAp' require statement necesarrylocal LuaUnit = require('btd.lua.test.lua)()
...
TestFunction = wrapFunctions('test1','test2','test3'
jEdit integration
Instructions for jEdit with 'console' and 'errorlist' plugins :
- Goto 'Plugins/Plugin Options/Console/Error Patterns'
- Add new Error Pattern '+'
- Enter Error Regexp: 'Failed:(.*?):(\d*?):(.*)'
- Enter Extra lines regexp: '(expected.*)'
- Enter Filename: '$1'
- Enter Line number: '$2'
- Enter Error message: '$3'