ExUnit - Generate test cases dynamically inside a describe block

Here is the use case that I am trying to solve:

  • I have a standard test module which has a setup_all method and a describe block.
  • Now, I make an API call in the setup_all method and set an array of values in the context.
  • I would like to iterate over these array of values and generate test cases for every value in the array.

Is there any way to achieve this?

Hence, ideally, I would like to receive the “context” in the describe macro and then “map” the array of values over the test macro. However, the describe block does not have access to the context which I have set in the setup_all function.

Or else, I do have a context (array of values) inside the test. Then, I should be able to dynamically iterate over this array of values and define test cases dynamically inside this test. Thus, it would be nested test cases.

The problem I am trying to solve is to avoid defining so many test cases. This is because the array of values will be close to 30 and it will just have the same “exact” code.

Not sure if it will always work, but you could try moving your API call outside of any setup block, and assign the array of values to a module attribute. Then for value <- @values do the test definition. Test name may need to be unique. Because test files are .exs the api call would run when you run the tests I think.

But I would probably use one test, eventually you could compute what would be expected vs actual for all test inputs and then compare a big map to have always a complete diff in the assertion

There is no such feature in ExUnit, I am a trying to write something like that, but for now what you can do is:

for value <- YourModule.generate_test_data() do
  test "check if #{value} is frindricated" do
    assert frindricated?(unquote(value))

Alternatively you can do:

setup_all do
  {:ok, values: YourModule.generate_test_data()}

test "check if all values are frindricated", %{values: values} do
  assert Enum.all?(values, &frindricated?/1)

Or you can use test tags for that:

@tag values: YourModule.generate_test_data()
test "check if all values are frindricated", %{values: values} do
  assert Enum.all?(values, &frindricated?/1)

yup, basically what I suggested but these examples look like they will compile :slight_smile:

1 Like