From 34f32fcb7b1ea567c60c9ae1f46a235e0fba4bfe Mon Sep 17 00:00:00 2001 From: Romain Forlot Date: Mon, 9 Jul 2018 11:54:26 +0200 Subject: Adds before and after function to set context 3 Cases are done: - before and after all tests - before and after each tests - before and after specific tests Change-Id: Ie222e6a3809a14d71a822b084a27ed93ec7bb286 Signed-off-by: Romain Forlot --- conf.d/controller/lua.d/aft.lua | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'conf.d/controller/lua.d/aft.lua') diff --git a/conf.d/controller/lua.d/aft.lua b/conf.d/controller/lua.d/aft.lua index bd0141f..d2587fe 100644 --- a/conf.d/controller/lua.d/aft.lua +++ b/conf.d/controller/lua.d/aft.lua @@ -252,22 +252,32 @@ function _AFT.testVerbError(testName, api, verb, args, cb) end}) end -function _AFT.describe(testName, testFunction) +function _AFT.describe(testName, testFunction, setUp, tearDown) 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 - function aTest:setUp() b() end - function aTest:tearDown() a() end + function aTest:setUp() + if type(setUp) == 'function' then setUp() end + b() + end + function aTest:tearDown() + a() + if type(tearDown) == 'function' then tearDown() end + end table.insert(_AFT.tests_list, {testName, function() - testFunction() + if type(testFunction) == 'function' then + testFunction() + else + print('# ERROR: Test '.. testName .. ' is note defined as a function.') + end end}) end -function _AFT.setbeforeEach(beforeEachTestFunction) +function _AFT.setBeforeEach(beforeEachTestFunction) if type(beforeEachTestFunction) == "function" then _AFT.beforeEach = beforeEachTestFunction else @@ -283,7 +293,7 @@ function _AFT.setAfterEach(afterEachTestFunction) end end -function _AFT.setBefore(beforeAllTestsFunctions) +function _AFT.setBeforeAll(beforeAllTestsFunctions) if type(beforeAllTestsFunctions) == "function" then _AFT.beforeAll = beforeAllTestsFunctions else @@ -291,7 +301,7 @@ function _AFT.setBefore(beforeAllTestsFunctions) end end -function _AFT.setAfter(afterAllTestsFunctions) +function _AFT.setAfterAll(afterAllTestsFunctions) if type(afterAllTestsFunctions) == "function" then _AFT.afterAll = afterAllTestsFunctions else @@ -440,7 +450,7 @@ function _launch_test(context, args) -- Keep the context unset function to be executed after all no matter if -- tests have been executed or not. if _AFT.afterAll then - if _AFT.afterAll() != 0 then + if _AFT.afterAll() ~= 0 then print('Unsetting the tests context failed.') end end -- cgit 1.2.3-korg