Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table driven tests support? #138

Open
davix opened this issue Mar 24, 2021 · 3 comments
Open

Table driven tests support? #138

davix opened this issue Mar 24, 2021 · 3 comments

Comments

@davix
Copy link

davix commented Mar 24, 2021

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
Copy link
Owner

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
Copy link
Author

davix commented Mar 24, 2021

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
Copy link

jjvbsag commented Nov 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants