diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2018-07-09 17:56:03 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-07-10 17:12:30 +0200 |
commit | d24dfa5264b1a15837afbec40aa25cd4422d3eb2 (patch) | |
tree | d114f176afe99e3b8bb1c297c8ea4c10a9ec9b1b /conf.d | |
parent | 75c0e78bd1214e5a62115ca4f4692287e238d954 (diff) |
2 ways for set up'before' and 'after' function
Either you specify the functions as a function argument
either you use a specific _AFT functions meant to add the
function to the test instance.
Change-Id: I4ccd467c70d2181d12edb354f80db6c233b8769d
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'conf.d')
-rw-r--r-- | conf.d/controller/lua.d/aft.lua | 97 | ||||
-rw-r--r-- | conf.d/controller/lua.d/helloworld.lua | 5 |
2 files changed, 65 insertions, 37 deletions
diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua index 9ba8909..329cefd 100644 --- a/conf.d/controller/lua.d/aft.lua +++ b/conf.d/controller/lua.d/aft.lua @@ -169,7 +169,7 @@ function _AFT.assertEvtReceived(eventName) end end -function _AFT.testEvtNotReceived(testName, eventName, timeout) +function _AFT.testEvtNotReceived(testName, eventName, timeout, setUp, tearDown) table.insert(_AFT.tests_list, {testName, function() if _AFT.beforeEach then _AFT.beforeEach() end if timeout then sleep(timeout) end @@ -178,7 +178,7 @@ function _AFT.testEvtNotReceived(testName, eventName, timeout) end}) end -function _AFT.testEvtReceived(testName, eventName, timeout) +function _AFT.testEvtReceived(testName, eventName, timeout, setUp, tearDown) table.insert(_AFT.tests_list, {testName, function() if _AFT.beforeEach then _AFT.beforeEach() end if timeout then sleep(timeout) end @@ -236,45 +236,70 @@ function _AFT.assertVerbError(api, verb, args, cb) end end -function _AFT.testVerb(testName, api, verb, args, cb) - table.insert(_AFT.tests_list, {testName, function() - if _AFT.beforeEach then _AFT.beforeEach() end +function _AFT.testVerb(testName, api, verb, args, cb, setUp, tearDown) + _AFT.describe(testName, function() _AFT.assertVerb(api, verb, args, cb) - if _AFT.afterEach then _AFT.afterEach() end - end}) + end, setUp, tearDown) end -function _AFT.testVerbError(testName, api, verb, args, cb) - table.insert(_AFT.tests_list, {testName, function() - if _AFT.beforeEach then _AFT.beforeEach() end - _AFT.assertVerbError(api, verb, args, cb) - if _AFT.afterEach then _AFT.afterEach() end - end}) +function _AFT.testVerbError(testName, api, verb, args, cb, setUp, tearDown) + _AFT.describe(testName, function() + _AFT.assertVerbError(api, verb, args, cb) + end, setUp, tearDown) + end function _AFT.describe(testName, testFunction, setUp, tearDown) - local sanitizedTestName = "test"..tostring(testName) if _AFT.beforeEach then local b = _AFT.beforeEach() end if _AFT.afterEach then local a = _AFT.afterEach() end local aTest = {} - function aTest:sanitizedTestName() testFunction() end + if type(testFunction) == 'function' then + function aTest:testFunction() testFunction() end + else + print('ERROR: #2 argument isn\'t of type function. Aborting...') + os.exit(1) + end function aTest:setUp() if type(setUp) == 'function' then setUp() end - b() + if b then b() end end function aTest:tearDown() - a() + if a then a() end if type(tearDown) == 'function' then tearDown() end end - table.insert(_AFT.tests_list, {testName, function() - if type(testFunction) == 'function' then - testFunction() - else - print('# ERROR: Test '.. testName .. ' is note defined as a function.') + table.insert(_AFT.tests_list, {testName, aTest}) +end + +function _AFT.setBefore(testName, beforeTestFunction) + if type(beforeTestFunction) == "function" then + for _,item in pairs(_AFT.test_list) do + if item[0] == testName then + item[1].setUp = function() + beforeTestFunction() + item[1].setUp() + end + end end - end}) + else + print("Wrong 'before' function defined. It isn't detected as a function type") + end +end + +function _AFT.setAfter(testName, afterTestFunction) + if type(afterTestFunction) == "function" then + for _,item in pairs(_AFT.test_list) do + if item[0] == testName then + item[1].setUp = function() + item[1].tearDown() + afterTestFunction() + end + end + end + else + print("Wrong 'after' function defined. It isn't detected as a function type") + end end function _AFT.setBeforeEach(beforeEachTestFunction) @@ -289,7 +314,7 @@ function _AFT.setAfterEach(afterEachTestFunction) if type(afterEachTestFunction) == "function" then _AFT.afterEach = afterEachTestFunction else - print("Wrong beforeEach function defined. It isn't detected as a function type") + print("Wrong afterEach function defined. It isn't detected as a function type") end end @@ -413,6 +438,18 @@ for _, v in pairs( _AFT_list_of_funcs ) do _AFT[alias] = _AFT[funcname] end +local function call_tests() + AFB:success(_AFT.context, { info = "Launching tests"}) + lu.LuaUnit:runSuiteByInstances(_AFT.tests_list) + + local success ="Success : "..tostring(lu.LuaUnit.result.passedCount) + local failures="Failures : "..tostring(lu.LuaUnit.result.testCount-lu.LuaUnit.result.passedCount) + + local evtHandle = AFB:evtmake(_AFT.context, 'results') + AFB:subscribe(_AFT.context,evtHandle) + AFB:evtpush(_AFT.context,evtHandle,{info = success.." "..failures}) +end + function _launch_test(context, args) _AFT.context = context @@ -433,18 +470,12 @@ function _launch_test(context, args) -- function success returning '0' else we abort the whole test procedure if _AFT.beforeAll then if _AFT.beforeAll() == 0 then - AFB:success(_AFT.context, { info = "Launching tests"}) - lu.LuaUnit:runSuiteByInstances(_AFT.tests_list) - - local success ="Success : "..tostring(lu.LuaUnit.result.passedCount) - local failures="Failures : "..tostring(lu.LuaUnit.result.testCount-lu.LuaUnit.result.passedCount) - - local evtHandle = AFB:evtmake(_AFT.context, 'results') - AFB:subscribe(_AFT.context,evtHandle) - AFB:evtpush(_AFT.context,evtHandle,{info = success.." "..failures}) + call_tests() else AFB:fail(_AFT.context, { info = "Can't set the context to execute the tests correctly. Look at the log and retry."}) end + else + call_tests() end -- Keep the context unset function to be executed after all no matter if diff --git a/conf.d/controller/lua.d/helloworld.lua b/conf.d/controller/lua.d/helloworld.lua index 1c2ec1d..17cfa84 100644 --- a/conf.d/controller/lua.d/helloworld.lua +++ b/conf.d/controller/lua.d/helloworld.lua @@ -32,7 +32,6 @@ end _AFT.addEventToMonitor("hello/anEvent") _AFT.addEventToMonitor("hello/anotherEvent", _callbackEvent) -_AFT.addLogToMonitor("hello", "warning", "verbose called for My Warning message!") _AFT.testVerbStatusSuccess('testPingSuccess','hello', 'ping', {}) _AFT.testVerbResponseEquals('testPingSuccessAndResponse','hello', 'ping', {}, "Some String") @@ -57,8 +56,6 @@ _AFT.testVerbStatusSuccess('testGenerateWarning', 'hello', 'verbose', {level = 4 _AFT.testEvtReceived("testanEventReceived", "hello/anEvent") _AFT.testEvtReceived("testanotherEventReceived", "hello/anotherEvent") -_AFT.testLogReceived("LogReceived", "verbose called for My Warning message!") - -_AFT.testCustom("mytest", function() +_AFT.describe("mytest", function() _AFT.assertEquals(false, false) end) |