Testing with EUnit
| Assertions | |
| Test modules | |
| Run tests | |
| Fixtures and setup |
Assertions
The EUnit macros assert facts about values. The question-mark prefix marks them as macros, and a failed assertion names the line and both values.
Assert equal compares two values, and assert checks a plain boolean expression.
-include_lib("eunit/include/eunit.hrl").
double_test() ->
?assertEqual(4, double(2)).
Test modules
A test module lives in test and ends in _tests.erl. The ifdef guard keeps the test code out of production builds.
Each test function answers ok when it passes, and raises when it fails.
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
area_test() ->
?assertEqual(9, area({square, 3})).
-endif.
Run tests
The eunit task in rebar3 compiles the tests and runs every function whose name ends in _test or _test_ in every module.
The report prints a dot per test and a summary at the end.
rebar3 eunit
Next: Debugging
Article author: Arthur Isaev