ct_hooks(3)
π NAME π
ct_hooks β a callback interface on top of Common Test, fr fr. (Erlang Module Definition, iykyk π
)
πΈ DESCRIPTION πΈ
okay so bestie, listen. The Common Test Hook (CTH) framework lets you extend the default behavior of Common Test by hooking in callbacks before and after all test suite calls. It's giving advanced-user energy β it's meant for those of you who want to abstract out behavior that's common to multiple test suites, and honestly? iconic.
In brief, CTH allows you to:
- Manipulate the runtime configuration before each suite configuration call. β¨
- Manipulate the return of all suite configuration calls and, by extension, the result of the test themselves. π
The following sections describe the mandatory and optional CTH functions that Common Test calls during test execution. For more details, see section Common Test Hooks in the User's Guide.
For info about how to add a CTH to your suite, see section Installing a CTH in the User's Guide. π«Ά
NOTE: For a minimal example of a CTH, see section Example CTH in the User's Guide. It's short, it's cute, go look. π¦
π§ FUNCTIONS π§
π CALLBACKS π
id(Opts)
The Id identifies a CTH instance uniquely. No cap. If two CTHs return the same Id, the second CTH is literally ignored π and all the subsequent calls to the CTH are only ever made to the first instance. For the details, see section Installing a CTH in the User's Guide.
This function is not to have any side effects, because Common Test can call it multiple times. So keep it clean, bestie.
If you don't implement it, the CTH acts as if this function returned a call to make_ref/0.
init(Id, Opts)
This function is always called before any other callback function. Use it to initiate any common state. It is to return a state for this CTH.
Id is either the return value of ct_hooks:id/1, or a reference (created using erlang:make_ref/0 in ERTS) if ct_hooks:id/1 is not implemented.
Priority is the relative priority of this hook. Hooks with a lower priority are executed first. If no priority is specified, it is set to 0.
For details about hook execution order, see section CTH Execution Order in the User's Guide. π
For details about when init is called, see section CTH Scope in the User's Guide.
on_tc_fail(SuiteName, TestName, Reason, CTHState)
This function is called whenever a test case (or configuration function) fails. It's called after the post function is called for the failed test case. So basically:
- If
init_per_suitefails, this function is called afterpost_init_per_suite. π - If a test case fails, this function is called after
post_end_per_testcase.
If the failed test case belongs to a test case group, the second argument is a tuple {FuncName,GroupName} β otherwise it's only the function name.
The data that comes with Reason follows the same format as FailReason in event tc_done. For details, see section Event Handling in the User's Guide.
If Module:on_tc_fail/4 is not exported, common_test will attempt to call Module:on_tc_fail(TestName, Reason, CTHState) instead. This is for backwards compatibility. π«Ά
on_tc_skip(SuiteName, TestName, Reason, CTHState)
This function is called whenever a test case (or configuration function) is skipped. It's called after the post function is called for the skipped test case. Which means:
- If
init_per_groupis skipped, this function is called afterpost_init_per_group. π - If a test case is skipped, this function is called after
post_end_per_testcase.
If the skipped test case belongs to a test case group, the second argument is a tuple {FuncName,GroupName} β otherwise it's only the function name.
The data that comes with Reason follows the same format as events tc_auto_skip and tc_user_skip. For details, see section Event Handling in the User's Guide.
If Module:on_tc_skip/4 is not exported, common_test will attempt to call Module:on_tc_skip(TestName, Reason, CTHState) instead. This is for backwards compatibility. π
post_all(SuiteName, Return, GroupDefs)
This function is called after all/0. It's used to modify the set of test cases and test group to be executed β for instance to add or remove test cases and groups, change group properties, or even skip all tests in the suite. Main character energy, honestly. π
Returnis whatall/0returned, that is, a list of test cases and groups to be executed, or a tuple{skip,Reason}.GroupDefsis whatgroups/0or thepost_groups/2hook returned, that is, a list of group definitions.NewReturnis the possibly modified version ofReturn.
This function is called only if the CTH is added before init_per_suite is run. For details, see section CTH Scope in the User's Guide.
Notice that for CTHs that are installed by means of the suite/0 function, post_all/2 is called before the init/2 hook function. However, for CTHs that are installed by means of the CT start flag, the init/2 function is called first. That's the timeline, no cap.
NOTE: Prior to each test execution, Common Test does a simulated test run in order to count test suites, groups and cases for logging purposes. This causes the post_all/3 hook function to always be called twice. For this reason, side effects are best avoided in this callback. π©
post_end_per_group(SuiteName, GroupName, Config, Return, CTHState)
This function is called after end_per_group if it exists. It behaves the same way as post_init_per_suite, but for function end_per_group instead. β¨
If Module:post_end_per_group/5 is not exported, common_test will attempt to call Module:post_end_per_group(GroupName, Config, Return, CTHState) instead. This is for backwards compatibility. π«Ά
post_end_per_suite(SuiteName, Config, Return, CTHState)
This function is called after end_per_suite if it exists. It behaves the same way as post_init_per_suite, but for function end_per_suite instead.
post_end_per_testcase(SuiteName, TestcaseName, Config, Return, CTHState)
This function is called after end_per_testcase if it exists. It behaves the same way as post_end_per_suite, but for function end_per_testcase instead. π¦
If Module:post_end_per_testcase/5 is not exported, common_test will attempt to call Module:post_end_per_testcase(TestcaseName, Config, Return, CTHState) instead. This is for backwards compatibility. π
post_groups(SuiteName, GroupDefs)
This function is called after groups/0. It's used to modify the test group definitions β for instance to add or remove groups or change group properties. Glow-up for your groups, honestly. π
GroupDefsis whatgroups/0returned, that is, a list of group definitions.NewGroupDefsis the possibly modified version of this list.
This function is called only if the CTH is added before init_per_suite is run. For details, see section CTH Scope in the User's Guide.
Notice that for CTHs that are installed by means of the suite/0 function, post_groups/2 is called before the init/2 hook function. However, for CTHs that are installed by means of the CT start flag, the init/2 function is called first.
NOTE: Prior to each test execution, Common Test does a simulated test run in order to count test suites, groups and cases for logging purposes. This causes the post_groups/2 hook function to always be called twice. For this reason, side effects are best avoided in this callback. π©
post_init_per_group(SuiteName, GroupName, Config, Return, CTHState)
This function is called after init_per_group if it exists. It behaves the same way as post_init_per_suite, but for function init_per_group instead. β¨
If Module:post_init_per_group/5 is not exported, common_test will attempt to call Module:post_init_per_group(GroupName, Config, Return, CTHState) instead. This is for backwards compatibility. π«Ά
post_init_per_suite(SuiteName, Config, Return, CTHState)
This function is called after init_per_suite if it exists. It typically contains extra checks to ensure that all the correct dependencies are started correctly. Vibe check on your deps, basically. β
Return is what init_per_suite returned, that is, {fail,Reason}, {skip,Reason}, a Config list, or a term describing how init_per_suite failed.
NewReturn is the possibly modified return value of init_per_suite. To recover from a failure in init_per_suite, return ConfigList with the tc_status element removed. For more details, see Post Hooks in section "Manipulating Tests" in the User's Guide.
CTHState is the current internal state of the CTH.
This function is called only if the CTH is added before or in init_per_suite. For details, see section CTH Scope in the User's Guide. π
post_init_per_testcase(SuiteName, TestcaseName, Config, Return, CTHState)
This function is called after init_per_testcase if it exists. It behaves the same way as post_init_per_suite, but for function init_per_testcase instead. β¨
If Module:post_init_per_testcase/5 is not exported, common_test will attempt to call Module:post_init_per_testcase(TestcaseName, Config, Return, CTHState) instead. This is for backwards compatibility. π«Ά
pre_end_per_group(SuiteName, GroupName, EndData, CTHState)
This function is called before end_per_group if it exists. It behaves the same way as pre_init_per_suite, but for function end_per_group instead. π¦
If Module:pre_end_per_group/4 is not exported, common_test will attempt to call Module:pre_end_per_group(GroupName, EndData, CTHState) instead. This is for backwards compatibility. π
pre_end_per_suite(SuiteName, EndData, CTHState)
This function is called before end_per_suite if it exists. It behaves the same way as pre_init_per_suite, but for function end_per_suite instead.
pre_end_per_testcase(SuiteName, TestcaseName, EndData, CTHState)
This function is called before end_per_testcase if it exists. It behaves the same way as pre_end_per_suite, but for function end_per_testcase instead. π«Ά
...and here's the kicker for this one: it is absolutely not allowed to change the result of the test case by returning skip or fail tuples π
β deny it, refuse, it will not work. What it can do is insert items in Config that can be read later in end_per_testcase/2 or in post_end_per_testcase/5. So basically it slides little notes under the door for future-you to find πΈ.
If Module:pre_end_per_testcase/4 is not exported, common_test will attempt to call Module:pre_end_per_testcase(TestcaseName, EndData, CTHState) instead. Yep bestie, it's for backwards compatibility β it always is, isn't it ππ«Ά.
pre_init_per_group(SuiteName, GroupName, InitData, CTHState) π¦
Called before init_per_group if it exists β¨. It literally behaves the same way as pre_init_per_suite, except it's for function init_per_group instead. Same vibes, different group chat π¬.
And if Module:pre_init_per_group/4 is not exported, common_test will attempt to call Module:pre_init_per_group(GroupName, InitData, CTHState) instead. Backwards compatibility, again, iconic truly π.
pre_init_per_suite(SuiteName, InitData, CTHState) π
This one gets called before init_per_suite if it exists. It's giving very much "the initialization and logging that has to happen before init_per_suite is called" energy π§β¨. And here's the dramatic part: if {skip,Reason} or {fail,Reason} is returned, then init_per_suite and all test cases of the suite are skipped, and Reason gets printed in the overview log of the suite ππ.
SuiteNameβ the name of the suite to be run, duh π .InitDataβ the original configuration list of the test suite, or aSkipOrFailtuple if a previous CTH has returned this π.CTHStateβ the current internal state of the CTH. That's its whole lil moment β¨.Returnβ is the result of theinit_per_suitefunction. If it is{skip,Reason}or{fail,Reason}, theninit_per_suiteis never called β instead the initiation is considered to be skipped or failed, respectively π. And if aNewConfiglist is returned,init_per_suiteis called with thatNewConfiglist π. For more details, see section Pre Hooks in the User's Guide π.
Heads up bestie: this function is called only if the CTH is added before init_per_suite is run. For details, see section CTH Scope in the User's Guide πβ¨.
pre_init_per_testcase(SuiteName, TestcaseName, InitData, CTHState) πΈ
Called before init_per_testcase if it exists π«. It behaves the same way as pre_init_per_suite, but for function init_per_testcase instead. Yes, the pattern is patterning π¦.
If Module:pre_init_per_testcase/4 is not exported, common_test will attempt to call Module:pre_init_per_testcase(TestcaseName, InitData, CTHState) instead. You already knew it was for backwards compatibility ππ.
CTHs cannot be added here right now. That feature may be added in a later release, but it would right now break backwards compatibility. So no, not today, not here ππ«.
terminate(Hooks) π
This one is called at the end of a CTH scope β the grand finale, the last bow, the outro music fading in πβ¨. And the returned term? Ignored. Literally nobody is reading it, so don't pour your soul into it π π«Ά.
And that's the whole page, bestie, you made it to the end ππβ¨. This absolute bop of a manpage comes to you from Ericsson AB β common_test version 1.31.1, ct_hooks(3). Go forth and hook responsibly, you gorgeous nerd ππ¦π
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms