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

Data Provider model #589

Open
ktalebian opened this issue Mar 4, 2019 · 1 comment
Open

Data Provider model #589

ktalebian opened this issue Mar 4, 2019 · 1 comment

Comments

@ktalebian
Copy link

I'm looking to implement a DataProvider model, similar to to TestNG for Java testing. This is what I've done so far:

_G.dataProvider = function(test, ...)
	local arg = {...}
	local data = {}
	local provider = arg[1]

	if #arg == 1 then
		if type(provider) == 'table' then
			data = provider
		elseif type(provider) == 'function' then
			data = provider()
		else
			error('Unsupported type ' .. type(provider) .. ' provided')
		end
	else
		data = arg
	end

	for _, value in pairs(data) do
		test(table.unpack(value))
	end
end

Then a typical test would look like:

describe('util', function()
    -- Provider as a function
    local provider = function()
        return {
            {'expected-result1', 'arg1a', 'arg1b'},
            {'expected-result2', 'arg2a', 'arg2b'},
            ...
        }
    end
    
    -- Provider as a table
    local provider = {
        {'expected-result1', 'arg1a', 'arg1b'},
        {'expected-result2', 'arg2a', 'arg2b'},
        ...
    }
    
    dataProvider(function(expected, arg1, arg2)
        it('should run some test', function()
            assert.are.equal(expected, util.someMethod(arg1, arg2))
        end)
    end, provider)
    
    -- Or provider inline as multiple arguments
    dataProvider(function(expected, arg1, arg2)
        it('should run some test', function()
            assert.are.equal(expected, util.someMethod(arg1, arg2))
        end)
    end, {'expected', 'arg1', 'arg2'}, {'expected', 'arg3', 'arg4'})
end)

Looking to see if there are any better way of doing this with Busted, or if we can include this in the Busted framework natively.

@Tieske
Copy link
Member

Tieske commented Mar 4, 2019

My preferred approach is to keep these close together (if you have a large test file, then the definition at the top of the describe block get separated from the actual tests using them. In those cases I use another describe block to keep them scoped smaller, and closer together.

There is no clear preference for any of the solutions you gave. There is a caveat though, of you have another function perform the tests, then and actual test failure will always get a stack trace pointing to that function. Doesn't make a big difference, just adds a bit of cognitive load to the debugging process.

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

No branches or pull requests

2 participants