[erlang-questions] EUnit fixture: return of Setup

Richard Carlsson richardc@REDACTED
Sat Nov 3 13:08:05 CET 2007

Ludovic Coquelle wrote:
> I have some problem with the EUnit fixture {setup, Setup, Clearup,
> Instanciator}.
> From the doc, I understand that the Instanciator should get the value
> return by the Setup.
> But in the following case, the test fail because assertion is "undefined
> == foo".
> Am I missing something?
> simple_init() ->
>     foo.
> simple_close(R) ->
>     R.
> simple_do(R) ->
>     ?assert(R == foo).
> simple_test_() ->
>     {
>         setup,
>         fun simple_init/0,
>         fun simple_close/1,
>         fun simple_do/1
>     }.

It does get the value from Setup during the test phase, but there is a
previous phase which tries to enumerate the tests, and that will pass
a dummy value ('undefined') to avoid actually running the setup.

Note that the name of your 'simple_do/1' function is misleading: since
it takes a parameter, EUnit assumes that it is (as you said) an
instantiator which should get the value from simple_init/0, and should
*return a test descriptor*, not try to run a test.

If you try the following, you will see what is happening:

simple_do(R) ->
  ?_assert(R == foo).  % note the underscore: return test, don't run it

(simple_do/1 is run twice - the first time with R='undefined').

Although EUnit is powerful and compact to write, I know that it
can be hard to keep in mind whether you should currently be writing
test-running code of test-generating code.


 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham

More information about the erlang-questions mailing list