Erlang basics · Lesson 24 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · INTERMEDIATE · LESSON 2425 minutes

Test small rules and whole flows

Use EUnit for focused functions and Common Test for multi-step scenarios.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Erlang
%% 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)).
Check the result first
  • rebar3 eunit reports one passing test when both boundary assertions hold.
02 · Take the code apart

Read from the first line down

  1. Read ?assertEqual: Compare an expected value with the actual value in EUnit.
  2. Read -include_lib("eunit/include/eunit.hrl").: Load EUnit assertion macros.
  3. Read all/0: List Common Test cases in a suite.
03 · Meet the new symbols

Symbols are not secret signs

?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.

04 · Ideas inside the code

Match each name to its meaning

01

unit test

A unit test checks one small behavior with little setup.

02

test suite

A Common Test suite groups scenarios and optional setup or cleanup callbacks.

03

boundary case

A boundary case sits where behavior changes, such as 59 versus 60.

05 · Make the idea clear

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.

06 · Change it yourself

Close the answer and try

Add a negative-score assertion.

Practice starting point
?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
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.

Take with you

Remember these three lines

  1. 1

    Use EUnit for small rules.

  2. 2

    Test both sides of boundaries.

  3. 3

    Use Common Test for larger scenarios.

Lesson completeRun the code once, then mark this lesson as done.