bluebird75 / luaunit

LuaUnit is a popular unit-testing framework for Lua, with an interface typical of xUnit libraries (Python unittest, Junit, NUnit, ...). It supports several output formats (Text, TAP, JUnit, ...) to be used directly or work with Continuous Integration platforms (Jenkins, Maven, ...).
Other
572 stars 137 forks source link

Table driven tests support? #138

Open davix opened 3 years ago

davix commented 3 years ago

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

bluebird75 commented 3 years ago

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.

davix commented 3 years ago

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.

jjvbsag commented 1 year ago

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() )