joshio1

joshio1

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.

First 3 of 3 Posts Switch mode

michallepicki

michallepicki

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

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 #2
michallepicki

michallepicki

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement