ExUnit setup with var

Hi all

Could someone please explain me, for what the argument var is in ExUnit setup function https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html#setup/2.

Thanks

2 Likes

So, as shown in the docs, there are two parameters to setup. The first one is var and the second one is block. Its usage is like so:

setup context do
...
end

This means the first argument used for the var parameter is context and the argument used for the block parameter is do ... end.

var is used to store data which may be useful in some test cases, thus the usage of context since it’s what it does, it stores context which can be useful for testing.

Also from the docs:

Context
If you return {:ok, keywords} from setup_all, the keyword will be merged into the current context and be available in all subsequent setup_all, setup and the test itself.

Similarly, returning {:ok, keywords} from setup, the keyword returned will be merged into the current context and be available in all subsequent setup and the test itself.

Returning :ok leaves the context unchanged in both cases.

Returning anything else from setup_all will force all tests to fail, while a bad response from setup causes the current test to fail.

1 Like