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 PARAMETERSor from a script :
local LuaUnit = require('btd.lua.test)() ... 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)() ... LuaUnit.errorIDE = true -- default = false
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)() ... LuaUnit.verbosity = false -- default = true
- help
- Display a help message, only makes sense when run from the shell. Displayed with '-help', '-h' or '-?'.
- silent
-
Turn off report output (default = on), only makes sense
when run from a script. It enables running different
testcases, gathering the output and reporting all results
in one report. From a script it can be turned on as follows :
local LuaUnit = require('btd.lua.test)() ... LuaUnit.silent = true -- default = false LuaUnit:run(testcases1) -- no report LuaUnit:run(testcases2) -- no report ... LuaUnit:report() -- report with (sorted) results of testcases1 and testcases2
- COMMANDS
-
- run
-
The actual test runner. Will be run automatically when
started from the shell, or can be started from a script
as follows :
local LuaUnit = require('btd.lua.test)() ... LuaUnit:run(testcases1)
Following parameters are supported :- no parameters DEPRECATED
- All test classes and test methods now run in their own environment. This is incompatible with the old functionality of scanning the global environment for test classes and/or methods.
- 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)() ... 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)() ... LuaUnit:run('Test1:test1','Test2:test2')
- report
-
When in
silent
mode a report can be triggered by this method. Only the results which have not been output yet will be reported.local LuaUnit = require('btd.lua.test)() LuaUnit.silent = true LuaUnit:run(testcases1) LuaUnit:run(testcases2) LuaUnit:report() -- will report results from testcases1 and testcases2
butlocal LuaUnit = require('btd.lua.test)() LuaUnit:run(testcases1) -- report results testcase1 LuaUnit:run(testcases2) -- report results testcas2 LuaUnit:report() -- will report nothing > reports generated after every LuaUnit:run(...)
- reportAll
-
Reports all gathered test results.
local LuaUnit = require('btd.lua.test)() LuaUnit:run(testcases1) -- report results testcase1 LuaUnit:run(testcases2) -- report results testcas2 LuaUnit:reportAll() -- will report results from testcase1 and testcase2
- clear
- Clear the test statistics. While in one session (of the stand alone interpreter for example) test statitics are accumulated.
- clearAll DEPRECATED
- The global environment is not scanned anymore for test classes and/or methods.
- remarks
-
-
All test classes and/or test methods parameters should be
reachable by a corresponding require statement.
local test = require('btd.lua.testapi)() ... LuaUnit:run('Test1:test1') -- 'Test1.lua' can be found by require LuaUnit:run('sub1.sub2.Test2') -- 'sub1/sub2/Test2.lua' can be found by require
-
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 run randomly
- Tests results 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 classfunction
fortestXXX
methods -
LuaUnit:run('function:method')
will run thefunction:method()
test
-
- when giving command line SWITCHES these are to come before the PARAMETERS
- command line switches '-v' and '-e' may be combined
-
All test classes and/or test methods parameters should be
reachable by a corresponding require statement.
- 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.
- Remarks
-
-
It is possible to pass variables between
setUp
,tearDown
and the test methods.local test = require('btd.lua.test)() ... TestClass = {} TestClass:setUp() self.variable = value -- initialise a variable end TestClass:testMethod() test.equals(function(self.variable),value2) -- use initialised variable end
-
It is not possible to use
self
insetUpClass
ortearDownClass
-
It is possible to pass variables between
local LuaUnit = require('btd.lua.test)() ... LuaUnit:run('Test1:test1','Test1:test2','Test3')
Will run (actual order of test methods might differ) :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()
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'