I'm trying to do something like this to not have to write manually a series of test blocks:
test_cases = %{
"foo" => 1,
"bar" => 2,
"baz" => 3,
}
Enum.each(test_cases, fn({input, expected_output}) ->
test "for #{input}" do
assert(Mymodule.myfunction input) == expected_output
end
end)
But when running this code I get the error undefined function input/0 on the line assert(Mymodule.myfunction input) == expected_output.
Is there a way to achieve what I want?
Yes it's possible, you just have to unquote both input and expected_output inside the do block that you pass to test/2.
test_cases = %{
"foo" => 1,
"bar" => 2,
"baz" => 3,
}
Enum.each test_cases, fn({input, expected_output}) ->
test "for #{input}" do
assert Mymodule.myfunction(unquote(input)) == unquote(expected_output)
end
end
Btw, you had a parens error in the assert line as you were calling assert/1 with just Mymodule.myfunction input as its argument, instead of Mymodule.myfunction(input) == expected_output (which is the expression you're trying to assert on).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With