Common Test
EUnit tests one function at a time and runs fastest inside the module. Common Test runs a suite of cases against a started system and writes an HTML report of every step. A large Erlang project uses both: EUnit for the unit, Common Test for the integration. This lesson writes a suite with one module under test, runs it with ct_run, and reads the summary.
A suite
A suite is a module whose name ends in _SUITE. It exports all/0, which answers the list of cases to run, and one function per case, each of arity one. The case receives the configuration and fails by raising, exactly as an EUnit test does. The module under test is ordinary code:
shop.erl
-module(shop). -export([sum/1, mean/1]). sum(Prices) -> lists:sum(Prices). mean(Prices) -> sum(Prices) / length(Prices).
shop_SUITE.erl
-module(shop_SUITE). -export([all/0, init_per_suite/1, end_per_suite/1, sum_case/1, mean_case/1]). all() -> [sum_case, mean_case]. init_per_suite(Config) -> [{prices, [1, 2, 3]} | Config]. end_per_suite(_Config) -> ok. sum_case(Config) -> Prices = proplists:get_value(prices, Config), 6 = shop:sum(Prices). mean_case(Config) -> Prices = proplists:get_value(prices, Config), 2.0 = shop:mean(Prices).
all/0 and the config
all/0 is the reading order of the suite: Common Test runs the
cases in the order the list gives, which is why a suite that shares state lists the
setup case first. The configuration is a property list. The framework puts its own keys
in it, and a suite adds its own with init_per_suite/1, which
receives the list and answers a new one; every case then reads the keys with
proplists:get_value/2.
end_per_suite/1 runs after the last case and releases what the
setup opened. A suite that starts a node, a socket or a table in the setup closes it
here, or the next run meets the leftovers. The names are fixed: the framework looks for
init_per_suite, end_per_suite,
init_per_testcase and end_per_testcase,
and a case that needs no setup simply ignores its argument.
Run it
Compile the module and the suite, make the log folder, and run the suite with ct_run. The compiler needs the suite in the same folder as the module it calls, or the module on the code path:
erlc shop.erl shop_SUITE.erl
mkdir -p logs
ct_run -noshell -dir . -suite shop_SUITE -logdir logs
TEST INFO: 1 test(s), 2 case(s) in 1 suite(s) Testing er.shop_ct.shop_SUITE: Starting test, 2 test cases Testing er.shop_ct.shop_SUITE: TEST COMPLETE, 2 ok, 0 failed of 2 test cases
The command is marked as not run on this page because it writes a folder whose name carries the node and the time, so a rerun never prints the same path. The three lines above are one real run in a folder called shop_ct; the er.shop_ct prefix is that folder, and the rest of the output names the log folder it created.
The report
The run writes logs/index.html, a page with a link per run, and
inside the run folder a page per suite with one row per case. A failed case shows the
exception, the source position and the time it took, which is the report a CI job keeps
when a suite goes red. The same information is in the suite log, and the return value of
ct:run_test/1 is a tuple of counts, which a build script can
read without parsing HTML.
A suite run from rebar3 is rebar3 ct. The
rebar3 lesson shows the tasks, and the
continuous integration
lesson puts the suite in the job that runs on every change.
The case that is not exported
Common Test calls a case by its name from all/0, so the case must
be exported. A case that is listed and not exported fails with
undef before its body runs, and the report names the missing
function rather than a wrong value. The same failure comes from a typo in
all/0: the list says sum_case and the
function is sum_cases.
The other mistake is a case that returns a value instead of raising. A case passes when
it returns, whatever it returns, so a case that computes a wrong number and returns it
is green. The match 6 = shop:sum(Prices) is what makes the case
a test; the EUnit lesson shows the same rule
for a test function.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
all/0of a Common Test suite answer?Answer
The list of cases to run
all/0lists the cases, in the order the suite runs them. -
How does a Common Test case fail?
Answer
By raising an exception
A case passes when it returns; any exception is a failure and is written to the report.
-
Which function adds keys to the configuration before the cases run?
Answer
init_per_suite/1init_per_suite/1receives the configuration and answers the one the cases see. -
What does
ct_runwrite?Answer
A log folder with an HTML report
The run writes a timestamped folder under the log directory, with an index and a page per suite.
-
What does this module print?
-module(q1). -export([main/0]). main() -> Config = [{prices, [2, 4, 6]}], Prices = proplists:get_value(prices, Config), io:format("~p~n", [lists:sum(Prices)]).Answer
12The config holds the list, and
shop:sum/1adds 2, 4 and 6. -
What does this module print?
-module(q2). -export([main/0]). main() -> Config = [{prices, [1, 2, 3]}], Prices = proplists:get_value(prices, Config), io:format("~p~n", [lists:sum(Prices) / length(Prices)]).Answer
2.0The mean is the sum 6 divided by the length 3, which
/computes as a float. -
What does this module print?
-module(q3). -export([main/0]). all() -> [sum_case, mean_case]. main() -> io:format("~p~n", [all()]).Answer
[sum_case,mean_case]all/0answers the list of case names in the order written. -
What does this module print?
-module(q4). -export([main/0]). main() -> Config = [{prices, [1, 2, 3]}], Prices = proplists:get_value(prices, Config), Result = try 6 = lists:sum(Prices), ok catch error:{badmatch, Value} -> {badmatch, Value} end, io:format("~p~n", [Result]).Answer
okThe sum is 6, the match succeeds and returns
ok, so the case would pass. -
What happens when
all/0names a case that is not exported?Answer
The case fails with
undefCommon Test calls the function by name, so an unexported case is an undefined function.
-
Why does a Common Test case need a match such as
6 = shop:sum(Prices)?Answer
To fail when the value is wrong
A case passes on any return value; the match is what turns a wrong value into a failure.
The suite and the module were compiled with Erlang/OTP 27 (erts-15.2.7.14) and run with Common Test 1.27.7.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container; the ct_run line was run there and is marked not run on this page because its log path changes.
Article author: Arthur Isaev