Is it possible that the ExUnit.test instruction inside Enum.each

I am trying to do something like this so as not to manually write multiple blocks test:

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 I run this code, I get an error undefined function input/0in the line assert(Mymodule.myfunction input) == expected_output.

Is there any way to achieve what I want?

+4
source share
1 answer

Yes, perhaps you only need unquoteboth input, and expected_outputin the block dothat you pass on 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 string assert, since you assert/1only called with Mymodule.myfunction inputas your argument instead Mymodule.myfunction(input) == expected_output(which is the expression you are trying to assert).

+6
source

Source: https://habr.com/ru/post/1625973/


All Articles