Erlang basics · Lesson 17 (open contents)
Put one tested module in Rebar3
Create an Erlang application and run a focused EUnit test.
Run this first
Run it once, change one input, and compare the new result.
%% src/score_card.erl
-module(score_card).
-export([pass/1]).
-include_lib("eunit/include/eunit.hrl").
pass(Score) when is_integer(Score), Score >= 60, Score =< 100 -> true;
pass(_Score) -> false.
%% EUnit discovers functions whose names end in _test
pass_test() ->
?assertEqual(true, pass(70)).- After
rebar3 new app score_card, save the module and runrebar3 eunit; the focused test passes.
Read from the first line down
- Read
rebar3 new app: Create an OTP application project. - Read
rebar3 compile: Compile project modules. - Read
rebar3 eunit: Run EUnit tests.
Symbols are not secret signs
rebar3 new appCreate an OTP application project.
rebar3 compileCompile project modules.
rebar3 eunitRun EUnit tests.
Match each name to its meaning
Rebar3
Rebar3 creates Erlang projects, resolves dependencies, compiles code, and runs tests.
EUnit
EUnit discovers small test functions and assertion macros.
profile
Rebar3 profiles keep test or development dependencies out of production builds.
Why these forms are useful
Run the example first. Predict one result, then change one input and run it again.
Rebar3 creates Erlang projects, resolves dependencies, compiles code, and runs tests.
EUnit discovers small test functions and assertion macros.
Close the answer and try
Write an EUnit assertion that 70 passes.
pass_test() ->
?assertEqual(____, score_card:pass(70)).Target result: Expect true.
Stuck? Read one hint
EUnit macros come from eunit.hrl.
After you run it, see one answer
pass_test() ->
?assertEqual(true, score_card:pass(70)).Think about it: Should generated `_build/` files be committed?
No. Rebar3 regenerates build output from source and configuration.
Remember these three lines
- 1
Use Rebar3 for repeatable projects.
- 2
Keep source under
src/. - 3
Run EUnit from the project root.