Skip to main content

W0094 - Redundant Common Test callback

Example​

-module(my_SUITE).
-export([all/0, init_per_suite/1, end_per_suite/1, my_test/1]).

all() -> [my_test].

init_per_suite(Config) -> Config.
%% <- warning: W0094: Callback init_per_suite/1 only does what Common Test does when it is missing.
end_per_suite(_Config) -> ok.
%% <- warning: W0094: Callback end_per_suite/1 only does what Common Test does when it is missing.

my_test(_Config) -> ok.

Explanation​

The init_per_suite/1, init_per_group/2 and init_per_testcase/2 callbacks are optional, and so are their end_per_* counterparts. When a suite defines neither callback of a pair, Common Test runs its own default instead: the init_per_* default returns the configuration unchanged and the end_per_* default returns ok. Common Test hooks still run around the defaults.

A callback is reported when it has a single clause, without guards, whose arguments are all variables, and which does the same as the default: an init_per_* callback that returns its configuration argument, or an end_per_* callback that returns ok.

Common Test calls its suite and group defaults only when a suite defines neither callback of the pair: a suite that defines init_per_suite/1 but not end_per_suite/1, for example, fails with undef. A suite that keeps an init_per_suite/1 or init_per_group/2 that does real work must therefore also keep the matching end_per_suite/1 or end_per_group/2, even when it only returns ok. Suite and group callbacks are only reported when every callback the suite defines for that pair does what the default does, so such a trivial end_per_* callback is not reported. The OTP pull request erlang/otp#12191 proposes to let suites define these callbacks independently; until a Common Test release includes it, the matching end_per_* callback is required.

Common Test skips a missing init_per_testcase/2 or end_per_testcase/2 on its own, so each of those is reported independently of the other.

Callbacks that are called or referenced, other than by their export entry and spec, are not reported, nor are callbacks required by a behaviour other than ct_suite.

Fix​

Delete the callback, together with its export entry and spec. For suite and group callbacks, delete both callbacks of the pair.