Erlang basics · Lesson 24 (open contents)
Test small rules and whole flows
Use EUnit for focused functions and Common Test for multi-step scenarios.
Run this first
Run it once, change one input, and compare the new result.
%% Keep both sides of the pass boundary together
-include_lib("eunit/include/eunit.hrl").
pass_boundary_test() ->
?assertEqual(true, score:pass(60)),
?assertEqual(false, score:pass(59)).rebar3 eunitreports one passing test when both boundary assertions hold.
Read from the first line down
- Read
?assertEqual: Compare an expected value with the actual value in EUnit. - Read
-include_lib("eunit/include/eunit.hrl").: Load EUnit assertion macros. - Read
all/0: List Common Test cases in a suite.
Symbols are not secret signs
?assertEqualCompare an expected value with the actual value in EUnit.
-include_lib("eunit/include/eunit.hrl").Load EUnit assertion macros.
all/0List Common Test cases in a suite.
Match each name to its meaning
unit test
A unit test checks one small behavior with little setup.
test suite
A Common Test suite groups scenarios and optional setup or cleanup callbacks.
boundary case
A boundary case sits where behavior changes, such as 59 versus 60.
Why these forms are useful
Run the example first. Predict one result, then change one input and run it again.
A unit test checks one small behavior with little setup.
A Common Test suite groups scenarios and optional setup or cleanup callbacks.
Close the answer and try
Add a negative-score assertion.
?assertEqual(____, score:check(-1)).Target result: Expect {error,out_of_range}.
Stuck? Read one hint
Use the public tagged error.
After you run it, see one answer
?assertEqual({error, out_of_range}, score:check(-1)).Think about it: When should you reach for Common Test?
Use it when a scenario needs several modules, setup steps, external resources, or suite-level lifecycle hooks.
Remember these three lines
- 1
Use EUnit for small rules.
- 2
Test both sides of boundaries.
- 3
Use Common Test for larger scenarios.