Parameterized testing with ExUnit

exunit
#1

I write an article Parameterized testing with ExUnit.The key concept is using ExUnit.Case.register_test/4 such as

ExUnit.start()

defmodule ParameterizedTest do
  use ExUnit.Case, async: true

  ExUnit.Case.register_attribute __ENV__, :pair

  for {lhs, rhs} <- [{"one", 1}, {"two", 2}, {"three", 3}] do
    @pair {lhs, rhs}

    test "#{lhs} convert to #{rhs}", context do
      {l, r} = context.registered.pair
      assert l === r
    end
  end

  test "pair should not have any value" do
    assert nil === @pair
  end
end
0 Likes

#2

I do not get why do you need attribute at all. Why not:

for {lhs, rhs} <- [{"one", 1}, {"two", 2}, {"three", 3}] do
  test "#{lhs} convert to #{rhs}" do
    assert unquote(lhs) == unquote(rhs)
  end
end
6 Likes

#3

Because I have never thought it until you mentioned.
It seems the code that you write is more smarter than I write. I use it.
Thanks a lot :joy:

3 Likes