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

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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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 49522 488
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement