Test small rules and whole flows: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Use EUnit for focused functions and Common Test for multi-step scenarios.
Write a boundary assertion in EUnit and recognize a Common Test suite callback.
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 eunit reports one passing test when both boundary assertions hold.
Steps for the full example
- 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.
Original lesson notes
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.
Symbols and terms
?assertEqual- Compare an expected value with the actual value in EUnit.
-include_lib("eunit/include/eunit.hrl").- Load EUnit assertion macros.
all/0- List Common Test cases in a suite.
- 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.
Original full exercise
Add a negative-score assertion.
?assertEqual(____, score:check(-1)).Expect {error,out_of_range}.
Show hint
Use the public tagged error.
Show original answer
?assertEqual({error, out_of_range}, score:check(-1)).When should you reach for Common Test?
Use it when a scenario needs several modules, setup steps, external resources, or suite-level lifecycle hooks.
Original lesson takeaways
- Use EUnit for small rules.
- Test both sides of boundaries.
- Use Common Test for larger scenarios.