joshio1
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.
Most Liked
hauleth
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))
end
end
Alternatively you can do:
setup_all do
{:ok, values: YourModule.generate_test_data()}
end
test "check if all values are frindricated", %{values: values} do
assert Enum.all?(values, &frindricated?/1)
end
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)
end
michallepicki
yup, basically what I suggested but these examples look like they will compile 







