return 42;

by Jan Wedel

eunit: Result from instantiator is not a test

I've just spent a few days finding out, why my test crashed with the eunit message:

Result from instantiator is not a test

I just found the reason by actually just looking at a similar test in my test suite. IMHO, eunit has some quirks that doesn't make life very easy. Especially when you were using junit (even though the language itself is questionable at best, junit is pretty good).

Acutally, I started to check the eunit sources to find out that eunit checks the return values from a test instanciator. When you want to pass a value to a test case, you need to use instanciators like that:

-module(device_producer_test).
-include_lib("eunit/include/eunit.hrl").

all_tests_test_() ->
    {inorder, {
        foreach,
            fun setup/0,
            fun teardown/1,
            [
                fun my_test/1
            ]
            }
        }.
setup() ->
    {ok, Pid} = some_server:start_link(),
    Pid.

my_test(Pid) ->
    Pid ! foo,
    ?assert(...)

I assumes, that the Pid returned from setup/0 will be passed to my_test/1. However, all_tests_test_ expects test initiators within the square brackets. my_testis expected to not be a test itself but to return a test function. If you want that in the test output you want to see the name of the test, you need to return that too. That results in the following code:

-module(device_producer_test).
-include_lib("eunit/include/eunit.hrl").

all_tests_test_() ->
    {inorder, {
        foreach,
            fun setup/0,
            fun teardown/1,
            [
                fun my_test/1
            ]
            }
        }.
setup() ->
    {ok, Pid} = some_server:start_link(),
    Pid.

my_test(Pid) ->
    [
        fun() ->
            Pid ! foo,
            ?assert(...)
        end
    ].