EUnit examples: Nested 'setup' and 'foreach'

2 Apr 2023 08:14 erlang eunit

If you want to run some setup before (and cleanup after) a list of tests, and also some setup and cleanup for each test, you can nest setup and foreach:

nested_setup_foreach_test_() ->
    {setup, fun before_all/0, fun after_all/1,
        {foreach, fun before_each/0, fun after_each/1, [
            fun something/0,
            fun another_thing/0

Here, I’ve used JUnit-style naming for the setup/cleanup functions.