The include file assert.hrl provides macros for inserting
assertions in your program code.
These macros are defined in the Stdlib include file
assert.hrl. Include the following directive in the module
from which the function is called:
-include_lib("stdlib/include/assert.hrl").
When an assertion succeeds, the assert macro yields the atom
ok. When an assertion fails, an exception of type error is
instead generated. The associated error term will have the form
{Macro, Info}, where Macro is the name of the macro, for
example assertEqual, and Info will be a list of tagged
values such as [{module, M}, {line, L}, ...] giving more
information about the location and cause of the exception. All entries
in the Info list are optional, and you should not rely
programatically on any of them being present.
If the macro NOASSERT is defined when the assert.hrl
include file is read by the compiler, the macros will be defined as
equivalent to the atom ok. The test will not be performed, and
there will be no cost at runtime.
For example, using erlc to compile your modules, the following
will disable all assertions:
erlc -DNOASSERT=true *.erl
(The value of NOASSERT does not matter, only the fact that it
is defined.)
A few other macros also have effect on the enabling or disabling of
assertions:
- If NODEBUG is defined, it implies NOASSERT, unless
DEBUG is also defined, which is assumed to take
precedence.
- If ASSERT is defined, it overrides NOASSERT, that
is, the assertions will remain enabled.
If you prefer, you can thus use only DEBUG/NODEBUG as
the main flags to control the behaviour of the assertions (which is
useful if you have other compiler conditionals or debugging macros
controlled by those flags), or you can use ASSERT/NOASSERT
to control only the assert macros.