[Proposal] setup_all block inside describe block

33 views
Skip to first unread message

Jose Vargas

unread,
Feb 12, 2023, 5:52:37 PM2/12/23
to elixir-lang-core
There is module-level setup and module-level setup_all
There is describe-level setup but there is no describe-level setup_all

I find it would be convenient to have describe level setup_all.

Right now the solution to have different setup_all for a group of tests is to break them into different modules. This works fine but sometimes I want those tests in the same module, perhaps I want them to share some module-level setup or setup_all.

So I propose a setup_all inside a describe block, same semantics as module-level but only affects describe block.
1. Runs only once for all the tests inside describe, runs in a separate process.
2. Receives context the same way as describe block setup.
3. Runs before describe block setup.

Thoughts?

Jose Vargas

unread,
Feb 12, 2023, 5:53:33 PM2/12/23
to elixir-lang-core
I forgot to tag the package: ExUnit.Callbacks

eksperimental

unread,
Feb 12, 2023, 8:29:42 PM2/12/23
to elixir-l...@googlegroups.com
On Sun, 12 Feb 2023 14:52:37 -0800 (PST)
Jose Vargas <josep...@gmail.com> wrote:

> There is module-level *setup* and module-level *setup_all*
> There is describe-level *setup* but there is no describe-level
> *setup_all*
>
> I find it would be convenient to have *describe* level *setup_all*.
>
> Right now the solution to have different *setup_all* for a group of
> tests is to break them into different modules. This works fine but
> sometimes I want those tests in the same module, perhaps I want them
> to share some module-level *setup* or *setup_all*.
>
> So I propose a *setup_all* inside a* describe* block, same semantics
> as module-level but only affects *describe* block.
> 1. Runs only once for all the tests inside *describe*, runs in a
> separate process.
> 2. Receives context the same way as *describe *block *setup*.
> 3. Runs before *describe* block *setup*.
>
> Thoughts?
>

eksperimental

unread,
Feb 12, 2023, 8:33:06 PM2/12/23
to elixir-l...@googlegroups.com
Hi Jose Vargas,

You can use tags as described in the `ExUnit.Callbacks` module docs:

Finally, as discussed in the ExUnit.Case documentation, remember that
the initial context metadata can also be set via @tags, which can then
be accessed in the setup block:

defmodule ExampleTagModificationTest do
use ExUnit.Case

setup %{login_as: username} do
{:ok, current_user: username}
end

@tag login_as: "max"
test "tags modify context", context do
assert context[:login_as] == "max"
assert context[:current_user] == "max"
end
end

Cheers

On Sun, 12 Feb 2023 14:52:37 -0800 (PST)
Jose Vargas <josep...@gmail.com> wrote:

> There is module-level *setup* and module-level *setup_all*
> There is describe-level *setup* but there is no describe-level
> *setup_all*
>
> I find it would be convenient to have *describe* level *setup_all*.
>
> Right now the solution to have different *setup_all* for a group of
> tests is to break them into different modules. This works fine but
> sometimes I want those tests in the same module, perhaps I want them
> to share some module-level *setup* or *setup_all*.
>
> So I propose a *setup_all* inside a* describe* block, same semantics
> as module-level but only affects *describe* block.
> 1. Runs only once for all the tests inside *describe*, runs in a
> separate process.
Reply all
Reply to author
Forward
0 new messages