erlangeunit

Why does Eunit not require test functions to be exported?


I'm going through the EUnit chapter in Learn You Some Erlang and one thing I am noticing from all the code samples is the test functions are never declared in -export() clauses.

Why is EUnit able to pick these test functions up?


Solution

  • From the documentation:

    The simplest way to use EUnit in an Erlang module is to add the following line at the beginning of the module (after the -module declaration, but before any function definitions):

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

    This will have the following effect:

    • Creates an exported function test() (unless testing is turned off, and the module does not already contain a test() function), that can be used to run all the unit tests defined in the module

    • Causes all functions whose names match ..._test() or ..._test_() to be automatically exported from the module (unless testing is turned off, or the EUNIT_NOAUTO macro is defined)