EUnit examples: a test list

31 Mar 2023 08:15 erlang eunit

While you could specify each test with a single function, you can also use a test “instantiator” (or generator) and have it return a list.

list_of_test_() ->
        fun something/0,
        fun() -> ?assert(true) end,
        {"description of test", ?_assert(true)},
        {<<"another test">>, fun another_thing/0}

something() -> ?assert(true).
another_thing() -> ?assert(true).

Note that this is a test generator/instantiator, rather than a simple test. This is denoted by the underscore in the function name: ...test_(). This is required, and EUnit won’t always tell you if you forget.

This example also shows a few of the different ways that you can specify a test:

  • As a local function reference – fun something/0.
  • As an anonymous function – fun() -> ?assert(true) end.
  • Using one of the test-generator macros (the ones starting with an underscore) – ?_assert(true).
  • Using a description. This must be a string or a binary.
Tip: Start with ?assert(false) as your test, to make sure it’s actually being run.

Note that using anonymous functions results in tests which are, well, anonymous.

This sometimes makes it hard to figure out which test has failed. For example:

  2) list_of_test:-list_of_test_/0-fun-4-/0:8
     Failure/Error: ?assert(false)
       expected: true
            got: false

To get around this, consider using the variants that use named functions: fun something/0, or the descriptions.