[erlang-questions] How to structure EUnit tests [was: I Hate Unit Testing]

Adam Lindberg <>
Thu Jan 29 10:57:28 CET 2009


Thought it might be beneficial to branch this to a side discussion.


This is how I use EUnit at the moment. I always use debug compiling and define TEST whenever I want tests compiled into the code.

In myapp/src/foo.erl I put a small header file inclusion:

-ifdef(TEST).
-include("foo_test.hrl").
-endif.


And myapp/test/foo_tests.hrl looks like this:

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


foo_test_() ->
    [?_assertEqual(ok, public_function())].

bar_test_() ->
    [?_assertEqual(ok, private_function())].



The benefits of this are several
* Production code and test code are separated
* Production code can be compiled and distributed without test code
* Private functions can be tested easily
* Test code can grow large in a space where it doesn't matter


It feels fairly "black box" since the tests are in another file. But I think the most important thing is that I can test private functions with unit tests. It is also easier to see when a module grows too large, since only production code exists in it.


Cheers,
Adam



More information about the erlang-questions mailing list