luaunit icon indicating copy to clipboard operation
luaunit copied to clipboard

Table driven tests support?

Open davix opened this issue 3 years ago • 3 comments

Hi, I'm using table driven tests, where all input and expected result are in a table.

However, in luaunit, it's a single case, not one case per entry in the table (like golang).

Is there a way to run the table as one case per entry?

Thanks

davix avatar Mar 24 '21 12:03 davix

You are right, there is no support today for such case. I am thinking about adding it one day or the other. I call it paramterized tests.

If you feel like implementing it, please submit a PR, I'll be happy to look at it.

bluebird75 avatar Mar 24 '21 13:03 bluebird75

It seems one function can be one case, so if I create a anonymous function for each entry in the test table, will luaunit treat them separately as different cases?

Sorry, I'm a Lua newbie, can't implement the feature now.

davix avatar Mar 24 '21 22:03 davix

A suggestion for @davix

local lu=require"luaunit.luaunit"

-- data for testcases
local testcases=
{
	{a=1,b=1},
	{a=2,b=4},
	{a=3,b=9},
}

-- function to test one testcase
local function test_testcase(testcase)
	local a,b=testcase.a,testcase.b
	lu.assertEquals(b,a*a)
end

-- create luaunit compliant test
TestTestcases={}
for i,testcase in ipairs(testcases) do
	TestTestcases["Test_"..i]=function()test_testcase(testcase)end
end

-- run test
os.exit( lu.LuaUnit.run() )

jjvbsag avatar Nov 27 '22 08:11 jjvbsag