Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have ExUnit.test statement inside a Enum.each

Tags:

elixir

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?

like image 936
Florent2 Avatar asked Jan 26 '16 10:01

Florent2


1 Answers

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

like image 114
whatyouhide Avatar answered Nov 17 '22 15:11

whatyouhide