Skip to content

Instantly share code, notes, and snippets.

@erszcz
Last active May 7, 2018 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erszcz/74f49e1446f33c6fd0316f6fcc2cd61b to your computer and use it in GitHub Desktop.
Save erszcz/74f49e1446f33c6fd0316f6fcc2cd61b to your computer and use it in GitHub Desktop.
Erlang Common Test helper defining test-local setup/teardown
-module(ct_helper).
-compile([export_all]).
%% @doc Like ct:get_config/1, but calls error/1 if Key is not found / undefined.
%% This guarantees to fail fast if required config options are missing from
%% the config file, saving trouble with `undefined` value propagation.
%% Use alongside with the CT `require` mechanism.
%% See s2s_SUITE for an example.
-spec get_config(atom()) -> any().
get_config(Key) ->
Val = ct:get_config(Key),
Val == undefined andalso error({undefined, Key}),
Val.
test_specific_init(Module, CaseName, Config) ->
case erlang:function_exported(Module, CaseName, 2) of
true -> Module:CaseName(init, Config);
false -> Config
end.
test_specific_end(Module, CaseName, Config) ->
case erlang:function_exported(Module, CaseName, 2) of
true -> Module:CaseName(end_, Config);
false -> Config
end.
-module(example_SUITE).
-compile([export_all]).
%%
%% CT Preamble
%%
all() ->
[
example_test
].
init_per_testcase(CaseName, Config) ->
ct_helper:test_specific_init(?MODULE, CaseName, Config).
end_per_testcase(CaseName, Config) ->
ct_helper:test_specific_end(?MODULE, CaseName, Config).
%%
%% Tests
%%
example_test(init, Config) ->
ct:pal("init config: ~p", [Config]),
Config;
example_test(end_, Config) ->
ct:pal("end_ config: ~p", [Config]),
Config.
example_test(_) ->
{skip, "just a test example"}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment