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

Adam Lindberg adam@REDACTED
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:


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


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.


More information about the erlang-questions mailing list