1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>2010</year><year>2012</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</legalnotice>
<title>Common Test Hooks</title>
<prepared>Lukas Larsson</prepared>
<responsible>Lukas Larsson</responsible>
<docno></docno>
<approved></approved>
<checked></checked>
<date>2010-12-02</date>
<rev>PA1</rev>
<file>ct_hooks.sgml</file>
</header>
<module>ct_hooks</module>
<modulesummary>A callback interface on top of Common Test</modulesummary>
<description>
<p>The <em>Common Test Hook</em> (henceforth called CTH) framework allows
extensions of the default behaviour of Common Test by means of callbacks
before and after all test suite calls. It is meant for advanced users of
Common Test which want to abstract out behaviour which is common to
multiple test suites. </p>
<p>In brief, Common Test Hooks allows you to:</p>
<list>
<item>Manipulate the runtime config before each suite
configuration call</item>
<item>Manipulate the return of all suite configuration calls and in
extension the result of the test themselves.</item>
</list>
<p>The following sections describe the mandatory and optional CTH
functions Common Test will call during test execution. For more details
see <seealso marker="ct_hooks_chapter">Common Test Hooks</seealso> in
the User's Guide.</p>
<p>For information about how to add a CTH to your suite see
<seealso marker="ct_hooks_chapter#installing">Installing a CTH
</seealso> in the User's Guide.</p>
<note><p>See the
<seealso marker="ct_hooks_chapter#example">Example CTH</seealso>
in the User's Guide for a minimal example of a CTH. </p></note>
</description>
<section>
<title>CALLBACK FUNCTIONS</title>
<p>The following functions define the callback interface
for a Common Test Hook.</p>
</section>
<funcs>
<func>
<name>Module:init(Id, Opts) -> {ok, State} |
{ok, State, Priority}</name>
<fsummary>Initiates the Common Test Hook</fsummary>
<type>
<v>Id = reference() | term()</v>
<v>Opts = term()</v>
<v>State = term()</v>
<v>Priority = integer()</v>
</type>
<desc>
<p> MANDATORY </p>
<p>Always called before any other callback function.
Use this to initiate any common state.
It should return a state for this CTH.</p>
<p><c>Id</c> is the return value of
<seealso marker="#Module:id-1">id/1</seealso>, or a <c>reference</c>
(created using
<seealso marker="erts:erlang#make_ref-0">make_ref/0</seealso>)
if <seealso marker="#Module:id-1">id/1</seealso> is not implemented.
</p>
<p><c>Priority</c> is the relative priority of this hook. Hooks with a
lower priority will be executed first. If no priority is given,
it will be set to 0. </p>
<p>For details about when init is called see
<seealso marker="ct_hooks_chapter#scope">scope</seealso>
in the User's Guide.</p>
</desc>
</func>
<func>
<name>Module:pre_init_per_suite(SuiteName, InitData, CTHState) ->
Result</name>
<fsummary>Called before init_per_suite</fsummary>
<type>
<v>SuiteName = atom()</v>
<v>InitData = Config | SkipOrFail</v>
<v>Config = NewConfig = [{Key,Value}]</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {Return, NewCTHState}</v>
<v>Return = NewConfig | SkipOrFail</v>
<v>SkipOrFail = {fail, Reason} | {skip, Reason}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called before
<seealso marker="common_test#Module:init_per_suite-1">
init_per_suite</seealso> if it exists.
It typically contains initialization/logging which needs to be done
before init_per_suite is called.
If <c>{skip,Reason}</c> or <c>{fail,Reason}</c> is returned,
init_per_suite and all test cases of the suite will be skipped and
Reason printed in the overview log of the suite.</p>
<p><c>SuiteName</c> is the name of the suite to be run.</p>
<p><c>InitData</c> is the original config list of the test suite, or
a <c>SkipOrFail</c> tuple if a previous CTH has returned this.</p>
<p><c>CTHState</c> is the current internal state of the CTH.</p>
<p><c>Return</c> is the result of the init_per_suite function.
If it is <c>{skip,Reason}</c> or <c>{fail,Reason}</c>
<seealso marker="common_test#Module:init_per_suite-1">init_per_suite
</seealso> will never be called, instead the initiation is considered
to be skipped/failed respectively. If a <c>NewConfig</c> list
is returned, <seealso marker="common_test#Module:init_per_suite-1">
init_per_suite</seealso> will be called with that <c>NewConfig</c> list.
See <seealso marker="ct_hooks_chapter#pre">
Pre Hooks</seealso> in the User's Guide for more details.</p>
<p>Note that this function is only called if the CTH has been added
before init_per_suite is run, see
<seealso marker="ct_hooks_chapter#scope">CTH Scoping</seealso>
in the User's Guide for details.</p>
</desc>
</func>
<func>
<name>Module:post_init_per_suite(SuiteName, Config, Return, CTHState) ->
Result</name>
<fsummary>Called after init_per_suite</fsummary>
<type>
<v>SuiteName = atom()</v>
<v>Config = [{Key,Value}]</v>
<v>Return = NewReturn = Config | SkipOrFail | term()</v>
<v>SkipOrFail = {fail, Reason} | {skip, Reason} | term()</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewReturn, NewCTHState}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called after
<seealso marker="common_test#Module:init_per_suite-1">
init_per_suite</seealso> if it exists. It typically contains extra
checks to make sure that all the correct dependencies have
been started correctly.</p>
<p><c>Return</c> is what
<seealso marker="common_test#Module:init_per_suite-1">init_per_suite
</seealso> returned, i.e. {fail,Reason}, {skip,Reason}, a <c>Config</c>
list or a term describing how
<seealso marker="common_test#Module:init_per_suite-1">init_per_suite
</seealso> failed.</p>
<p><c>NewReturn</c> is the possibly modified return value of
<seealso marker="common_test#Module:init_per_suite-1">init_per_suite
</seealso>. It is here possible to recover from a failure in
<seealso marker="common_test#Module:init_per_suite-1">init_per_suite
</seealso> by returning the <c>ConfigList</c> with the <c>tc_status</c>
element removed. See <seealso marker="ct_hooks_chapter#post">
Post Hooks</seealso> in the User's Guide for more details.</p>
<p><c>CTHState</c> is the current internal state of the CTH.</p>
<p>Note that this function is only called if the CTH has been added
before or in init_per_suite, see
<seealso marker="ct_hooks_chapter#scope">CTH Scoping</seealso>
in the User's Guide for details.</p>
</desc>
</func>
<func>
<name>Module:pre_init_per_group(GroupName, InitData, CTHState) ->
Result</name>
<fsummary>Called before init_per_group</fsummary>
<type>
<v>GroupName = atom()</v>
<v>InitData = Config | SkipOrFail</v>
<v>Config = NewConfig = [{Key,Value}]</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewConfig | SkipOrFail, NewCTHState}</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called before
<seealso marker="common_test#Module:init_per_group-2">
init_per_group</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:pre_init_per_suite-3">
pre_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:init_per_group-2">
init_per_group</seealso> instead.</p>
</desc>
</func>
<func>
<name>Module:post_init_per_group(GroupName, Config, Return, CTHState) ->
Result</name>
<fsummary>Called after init_per_group</fsummary>
<type>
<v>GroupName = atom()</v>
<v>Config = [{Key,Value}]</v>
<v>Return = NewReturn = Config | SkipOrFail | term()</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewReturn, NewCTHState}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called after
<seealso marker="common_test#Module:init_per_group-2">
init_per_group</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:post_init_per_suite-4">
post_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:init_per_group-2">
init_per_group</seealso> instead.</p>
</desc>
</func>
<func>
<name>Module:pre_init_per_testcase(TestcaseName, InitData, CTHState) ->
Result</name>
<fsummary>Called before init_per_testcase</fsummary>
<type>
<v>TestcaseName = atom()</v>
<v>InitData = Config | SkipOrFail</v>
<v>Config = NewConfig = [{Key,Value}]</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewConfig | SkipOrFail, NewCTHState}</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called before
<seealso marker="common_test#Module:init_per_testcase-2">
init_per_testcase</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:pre_init_per_suite-3">
pre_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:init_per_testcase-2">
init_per_testcase</seealso> function instead.</p>
<p>Note that it is not possible to add CTH's here right now,
that feature might be added later,
but it would right now break backwards compatibility.</p>
</desc>
</func>
<func>
<name>Module:post_end_per_testcase(TestcaseName, Config, Return, CTHState)
-> Result</name>
<fsummary>Called after end_per_testcase</fsummary>
<type>
<v>TestcaseName = atom()</v>
<v>Config = [{Key,Value}]</v>
<v>Return = NewReturn = Config | SkipOrFail | term()</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewReturn, NewCTHState}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called after
<seealso marker="common_test#Module:end_per_testcase-2">
end_per_testcase</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:post_init_per_suite-4">
post_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:end_per_testcase-2">
end_per_testcase</seealso> function instead.</p>
</desc>
</func>
<func>
<name>Module:pre_end_per_group(GroupName, EndData, CTHState) ->
Result</name>
<fsummary>Called before end_per_group</fsummary>
<type>
<v>GroupName = atom()</v>
<v>EndData = Config | SkipOrFail</v>
<v>Config = NewConfig = [{Key,Value}]</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewConfig | SkipOrFail, NewCTHState}</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called before
<seealso marker="common_test#Module:end_per_group-2">
end_per_group</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:pre_init_per_suite-3">
pre_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:end_per_group-2">
end_per_group</seealso> function instead.</p>
</desc>
</func>
<func>
<name>Module:post_end_per_group(GroupName, Config, Return, CTHState) ->
Result</name>
<fsummary>Called after end_per_group</fsummary>
<type>
<v>GroupName = atom()</v>
<v>Config = [{Key,Value}]</v>
<v>Return = NewReturn = Config | SkipOrFail | term()</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewReturn, NewCTHState}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called after
<seealso marker="common_test#Module:end_per_group-2">
end_per_group</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:post_init_per_suite-4">
post_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:end_per_group-2">
end_per_group</seealso> function instead.</p>
</desc>
</func>
<func>
<name>Module:pre_end_per_suite(SuiteName, EndData, CTHState) ->
Result</name>
<fsummary>Called before end_per_suite</fsummary>
<type>
<v>SuiteName = atom()</v>
<v>EndData = Config | SkipOrFail</v>
<v>Config = NewConfig = [{Key,Value}]</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewConfig | SkipOrFail, NewCTHState}</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called before
<seealso marker="common_test#Module:end_per_suite-1">
end_per_suite</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:pre_init_per_suite-3">
pre_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:end_per_suite-1">
end_per_suite</seealso> function instead.</p>
</desc>
</func>
<func>
<name>Module:post_end_per_suite(SuiteName, Config, Return, CTHState) ->
Result</name>
<fsummary>Called after end_per_suite</fsummary>
<type>
<v>SuiteName = atom()</v>
<v>Config = [{Key,Value}]</v>
<v>Return = NewReturn = Config | SkipOrFail | term()</v>
<v>SkipOrFail = {fail,Reason} | {skip, Reason}</v>
<v>CTHState = NewCTHState = term()</v>
<v>Result = {NewReturn, NewCTHState}</v>
<v>Key = atom()</v>
<v>Value = term()</v>
<v>Reason = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called after
<seealso marker="common_test#Module:end_per_suite-1">
end_per_suite</seealso> if it exists. It behaves the same way as
<seealso marker="ct_hooks#Module:post_init_per_suite-4">
post_init_per_suite</seealso>, but for the
<seealso marker="common_test#Module:end_per_suite-1">
end_per_suite</seealso> function instead.</p>
</desc>
</func>
<func>
<name>Module:on_tc_fail(TestName, Reason, CTHState) ->
NewCTHState</name>
<fsummary>Called after the CTH scope ends</fsummary>
<type>
<v>TestName = init_per_suite | end_per_suite |
{init_per_group,GroupName} | {end_per_group,GroupName} |
{FuncName,GroupName} | FuncName</v>
<v>FuncName = atom()</v>
<v>GroupName = atom()</v>
<v>Reason = term()</v>
<v>CTHState = NewCTHState = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called whenever a test case (or config function)
fails. It is called after the post function has been called for
the failed test case. I.e. if init_per_suite fails, this function
is called after
<seealso marker="#Module:post_init_per_suite-4">
post_init_per_suite</seealso>, and if a test case fails, it is called
after <seealso marker="#Module:post_end_per_testcase-4">
post_end_per_testcase</seealso>. If the failed test case belongs
to a test case group, the first argument is a tuple
<c>{FuncName,GroupName}</c>, otherwise simply the function name.</p>
<p>The data which comes with the Reason follows the same format as the
<seealso marker="event_handler_chapter#failreason">FailReason
</seealso> in the <seealso marker="event_handler_chapter#tc_done">tc_done</seealso> event.
See <seealso marker="event_handler_chapter#events">Event Handling
</seealso> in the User's Guide for details.</p>
</desc>
</func>
<func>
<name>Module:on_tc_skip(TestName, Reason, CTHState) ->
NewCTHState</name>
<fsummary>Called after the CTH scope ends</fsummary>
<type>
<v>TestName = init_per_suite | end_per_suite |
{init_per_group,GroupName} | {end_per_group,GroupName} |
{FuncName,GroupName} | FuncName</v>
<v>FuncName = atom()</v>
<v>GroupName = atom()</v>
<v>Reason = {tc_auto_skip | tc_user_skip, term()}</v>
<v>CTHState = NewCTHState = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called whenever a test case (or config function)
is skipped. It is called after the post function has been called
for the skipped test case. I.e. if init_per_group is skipped, this
function is called after
<seealso marker="#Module:post_init_per_group-4">
post_init_per_group</seealso>, and if a test case is skipped,
it is called after
<seealso marker="#Module:post_end_per_testcase-4">
post_end_per_testcase</seealso>. If the skipped test case belongs to a
test case group, the first argument is a tuple <c>{FuncName,GroupName}</c>,
otherwise simply the function name.</p>
<p>The data which comes with the Reason follows the same format as
<seealso marker="event_handler_chapter#tc_auto_skip">tc_auto_skip
</seealso> and <seealso marker="event_handler_chapter#tc_user_skip">
tc_user_skip</seealso> events.
See <seealso marker="event_handler_chapter#events">Event Handling
</seealso> in the User's Guide for details.</p>
</desc>
</func>
<func>
<name>Module:terminate(CTHState)</name>
<fsummary>Called after the CTH scope ends</fsummary>
<type>
<v>CTHState = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>This function is called at the end of a CTH's
<seealso marker="ct_hooks_chapter#scope">scope</seealso>.
</p>
</desc>
</func>
<func>
<name>Module:id(Opts) -> Id</name>
<fsummary>Called before the init function of a CTH</fsummary>
<type>
<v>Opts = term()</v>
<v>Id = term()</v>
</type>
<desc>
<p> OPTIONAL </p>
<p>The <c>Id</c> is used to uniquely identify a CTH instance,
if two CTH's return the same <c>Id</c> the second CTH is ignored
and subsequent calls to the CTH will only be made to the first
instance. For more information see
<seealso marker="ct_hooks_chapter#installing">Installing a CTH
</seealso> in the User's Guide.
</p>
<p>This function should NOT have any side effects as it might
be called multiple times by Common Test.</p>
<p>If not implemented the CTH will act as if this function returned a
call to <c>make_ref/0</c>.</p>
</desc>
</func>
</funcs>
</erlref>
|