View Source ExUnitParameterize (ExUnitParameterize v0.1.0-alpha.4)

Parameterized tests for ExUnit.

Provides the parameterized_test macro, implementing test parameterization for ExUnit. This aims to behave just like the ExUnit.test macro (it actually uses the test macro, under the hood), but takes one extra argument, the list of parameters.

Each group of parameters in the list will generate a test. Parameters will get injected into the test's do block as variables. There are two ways to specify the names of the variables:

  • once for all the params, as a list of atoms
  • repeated for each group of params, by passing each params group as a keywords list

Examples:

defmodule ParameterizedTest do
  use ExUnit.Case
  import ExUnitParameterize

  # specify the var names once, then provide the parameters
  parameterized_test "vars once", [
    [:a, :b, :expected],
    [1, 1, 2],                # vars once[a:1, b:1, expected:2]
    one_plus_two: [1, 2, 3],  # vars once[one_plus_two]
    failing_case: [1, 2, 4]   # vars once[failing_case]
  ] do
    assert a + b == expected
  end

  # repeat the var names for each param group
  parameterized_test "vars repeated", [
    [a: 1, b: 1, expected: 2],                # vars repeated[a:1, b:1, expected:2]
    one_plus_two: [a: 1, b: 2, expected: 3],  # vars repeated[one_plus_two]
    failing_case: [a: 1, b: 2, expected: 4]   # vars repeated[failing_case]
  ] do
    assert a + b == expected
  end


end

Test naming

By default the string representation of the params will be appended to the test name, but you can provide an explicit name by passing the group of parameters as a keyword list.

For the example above the names for the "vars once" parameterized_test would be:

  • vars once[a: 1, b: 1, expected: 2]
  • vars once[one_plus_two]
  • vars once[failing_case]

In case the name would be longer than the max atom size, the 1-based index will be used.

Summary

Functions

Defines a not implemented parametrized test.

Defines a parameterized test that uses the test context.

Functions

Link to this macro

parameterized_test(message, parameters)

View Source (macro)

Defines a not implemented parametrized test.

See ExUnit.Case.test/1.

Examples:

defmodule NotImplementedCase do
  use ExUnit.Case
  import ExUnitParameterize
  parameterized_test "name", [
    [a: 1],
    [a: 2],
  ]
end
Link to this macro

parameterized_test(message, var \\ quote do _ end, parameters, contents)

View Source (macro)

Defines a parameterized test that uses the test context.

See ExUnit.Case.test/3 and ExUnit.Case#module-context

Examples:

defmodule ParameterizedCaseWithTagsAndContext do
  use ExUnit.Case
  import ExUnitParameterize

  setup do
    {:ok, spam: "spam"}
  end

  @tag foo_tag: "foo"
  @tag :bar_tag
  parameterized_test "basic test with tags and context", context, [
    [a: 1, b: 2, expected: 3],
    [a: 1, b: 2, expected: 4]
  ] do
    assert context[:foo_tag] == "foo"
    assert context[:bar_tag] == true
    assert context[:spam] == "spam"
    assert a + b == expected
  end

end