joshio1

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

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
12
Post #3
michallepicki

michallepicki

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

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement