fireproofsocks

fireproofsocks

Behavior of setup_all in ExUnit

I’ve been playing round with test fixtures using setup_all, and I’m considering opening a pull request which includes a few more concrete examples.

The specific behavior that I found to be a bit of a secret is that your test statements can receive values from the setup_all blocks (!!!). In addition to being a bit of a secret bit of functionality, I also had some confusion as to why the setup_all block could return either a map OR a tuple (with a map), and the test statement receives only the map. This is the type of “magic” or hidden functionality that can make code difficult to understand, so I would like to ask for some insight from the forum members on this topic.

Would adding the following as ## Examples in the documentation be useful?

defmodule ExampleATest do
  use ExUnit.Case

  setup_all do
    %{is_a_map: true}
  end

  test "this function receives the output of the setup_all callback", value do
    assert true == value[:is_a_map]
  end
end

defmodule ExampleBTest do
  use ExUnit.Case

  setup_all do
    [keyword: true]
  end

  test "keyword lists are also allowed", value do
    assert true == value[:keyword]
  end
end

defmodule ExampleCTest do
  use ExUnit.Case

  setup_all do
    {:ok, %{is_a_map: true}}
  end

  test "surprisingly, if an :ok tuple is returned, tests only receive the value", value do
    assert true == value[:is_a_map]
  end
end

Most Liked

dimitarvp

dimitarvp

defmodule EdnoTest do
  use ExUnit.Case

  setup_all do
    {:ok, setup_all: :rand.uniform(100)}
  end

  setup do
    {:ok, setup: :rand.uniform(100)}
  end

  test "1", context do
    IO.inspect Map.take(context, [:setup_all, :setup])
  end

  test "2", context do
    IO.inspect Map.take(context, [:setup_all, :setup])
  end
end

Running this test suite yields this:

%{setup: 85, setup_all: 6}
.%{setup: 44, setup_all: 6}
.

setup_all has the same value for all tests in the suite – namely it’s called once for the test file (the suite) – while setup is called on each test.

Not trying to be demeaning, just legitimately curious what is tripping you.

fireproofsocks

fireproofsocks

That’s a great example! Worthy of being included in the docs.

What’s tripping me is that the docs do not have examples as clear as that. I know I am an educator, so my standards for documentation may be high, but I feel the current docs could help reduce mental friction and clarify this feature more quickly by including more examples. Explanations are a nice-to-have; examples are paramount.

Specifically:

  • The docs nowhere mention the notion of a Test Fixture, and that’s really what these functions are relevant to. Other languages/frameworks refer to such functions as fixtures, so the mere act of name-dropping that term will help the light-bulb go off for some people.
  • The examples of the context variable could be more clear (e.g. like yours).
  • I haven’t seen a clear example of how/why you can return a tuple OR a map and the context comes through the same. That’s kind of a weird bit of unseen macro “magic” that is rare in the functional world.

I’m working on the PR to include some clarifications to the existing docs.

fireproofsocks

fireproofsocks

Yeah, I read that but I didn’t understand much of it from the examples given. “Your documentation has too many examples” said no developer ever. I’ll put together a PR and see what comes of it. Thanks!

Where Next?

Popular in Questions Top

myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement