1ATF-C-API(3)             BSD Library Functions Manual             ATF-C-API(3)
2

NAME

4     atf-c-api, ATF_CHECK, ATF_CHECK_MSG, ATF_CHECK_EQ, ATF_CHECK_EQ_MSG,
5     ATF_CHECK_MATCH, ATF_CHECK_MATCH_MSG, ATF_CHECK_STREQ,
6     ATF_CHECK_STREQ_MSG, ATF_CHECK_ERRNO, ATF_REQUIRE, ATF_REQUIRE_MSG,
7     ATF_REQUIRE_EQ, ATF_REQUIRE_EQ_MSG, ATF_REQUIRE_MATCH,
8     ATF_REQUIRE_MATCH_MSG, ATF_REQUIRE_STREQ, ATF_REQUIRE_STREQ_MSG,
9     ATF_REQUIRE_ERRNO, ATF_TC, ATF_TC_BODY, ATF_TC_BODY_NAME, ATF_TC_CLEANUP,
10     ATF_TC_CLEANUP_NAME, ATF_TC_HEAD, ATF_TC_HEAD_NAME, ATF_TC_NAME,
11     ATF_TC_WITH_CLEANUP, ATF_TC_WITHOUT_HEAD, ATF_TP_ADD_TC, ATF_TP_ADD_TCS,
12     atf_tc_get_config_var, atf_tc_get_config_var_wd,
13     atf_tc_get_config_var_as_bool, atf_tc_get_config_var_as_bool_wd,
14     atf_tc_get_config_var_as_long, atf_tc_get_config_var_as_long_wd,
15     atf_no_error, atf_tc_expect_death, atf_tc_expect_exit,
16     atf_tc_expect_fail, atf_tc_expect_pass, atf_tc_expect_signal,
17     atf_tc_expect_timeout, atf_tc_fail, atf_tc_fail_nonfatal, atf_tc_pass,
18     atf_tc_skip, atf_utils_cat_file, atf_utils_compare_file,
19     atf_utils_copy_file, atf_utils_create_file, atf_utils_file_exists,
20     atf_utils_fork, atf_utils_free_charpp, atf_utils_grep_file,
21     atf_utils_grep_string, atf_utils_readline, atf_utils_redirect,
22     atf_utils_wait — C API to write ATF-based test programs
23

SYNOPSIS

25     #include <atf-c.h>
26
27     ATF_CHECK(expression);
28
29     ATF_CHECK_MSG(expression, fail_msg_fmt, ...);
30
31     ATF_CHECK_EQ(expression_1, expression_2);
32
33     ATF_CHECK_EQ_MSG(expression_1, expression_2, fail_msg_fmt, ...);
34
35     ATF_CHECK_MATCH(regexp, string);
36
37     ATF_CHECK_MATCH_MSG(regexp, string, fail_msg_fmt, ...);
38
39     ATF_CHECK_STREQ(string_1, string_2);
40
41     ATF_CHECK_STREQ_MSG(string_1, string_2, fail_msg_fmt, ...);
42
43     ATF_CHECK_ERRNO(exp_errno, bool_expression);
44
45     ATF_REQUIRE(expression);
46
47     ATF_REQUIRE_MSG(expression, fail_msg_fmt, ...);
48
49     ATF_REQUIRE_EQ(expression_1, expression_2);
50
51     ATF_REQUIRE_EQ_MSG(expression_1, expression_2, fail_msg_fmt, ...);
52
53     ATF_REQUIRE_MATCH(regexp, string);
54
55     ATF_REQUIRE_MATCH_MSG(regexp, string, fail_msg_fmt, ...);
56
57     ATF_REQUIRE_STREQ(string_1, string_2);
58
59     ATF_REQUIRE_STREQ_MSG(string_1, string_2, fail_msg_fmt, ...);
60
61     ATF_REQUIRE_ERRNO(exp_errno, bool_expression);
62
63     ATF_TC(name);
64
65     ATF_TC_BODY(name, tc);
66
67     ATF_TC_BODY_NAME(name);
68
69     ATF_TC_CLEANUP(name, tc);
70
71     ATF_TC_CLEANUP_NAME(name);
72
73     ATF_TC_HEAD(name, tc);
74
75     ATF_TC_HEAD_NAME(name);
76
77     ATF_TC_NAME(name);
78
79     ATF_TC_WITH_CLEANUP(name);
80
81     ATF_TC_WITHOUT_HEAD(name);
82
83     ATF_TP_ADD_TC(tp_name, tc_name);
84
85     ATF_TP_ADD_TCS(tp_name);
86
87     atf_tc_get_config_var(tc, varname);
88
89     atf_tc_get_config_var_wd(tc, variable_name, default_value);
90
91     atf_tc_get_config_var_as_bool(tc, variable_name);
92
93     atf_tc_get_config_var_as_bool_wd(tc, variable_name, default_value);
94
95     atf_tc_get_config_var_as_long(tc, variable_name);
96
97     atf_tc_get_config_var_as_long_wd(tc, variable_name, default_value);
98
99     atf_no_error();
100
101     atf_tc_expect_death(reason, ...);
102
103     atf_tc_expect_exit(exitcode, reason, ...);
104
105     atf_tc_expect_fail(reason, ...);
106
107     atf_tc_expect_pass();
108
109     atf_tc_expect_signal(signo, reason, ...);
110
111     atf_tc_expect_timeout(reason, ...);
112
113     atf_tc_fail(reason);
114
115     atf_tc_fail_nonfatal(reason);
116
117     atf_tc_pass();
118
119     atf_tc_skip(reason);
120
121     void
122     atf_utils_cat_file(const char *file, const char *prefix);
123
124     bool
125     atf_utils_compare_file(const char *file, const char *contents);
126
127     void
128     atf_utils_copy_file(const char *source, const char *destination);
129
130     void
131     atf_utils_create_file(const char *file, const char *contents, ...);
132
133     void
134     atf_utils_file_exists(const char *file);
135
136     pid_t
137     atf_utils_fork(void);
138
139     void
140     atf_utils_free_charpp(char **argv);
141
142     bool
143     atf_utils_grep_file(const char *regexp, const char *file, ...);
144
145     bool
146     atf_utils_grep_string(const char *regexp, const char *str, ...);
147
148     char *
149     atf_utils_readline(int fd);
150
151     void
152     atf_utils_redirect(const int fd, const char *file);
153
154     void
155     atf_utils_wait(const pid_t pid, const int expected_exit_status,
156         const char *expected_stdout, const char *expected_stderr);
157

DESCRIPTION

159     ATF provides a C programming interface to implement test programs.  C-
160     based test programs follow this template:
161
162           ... C-specific includes go here ...
163
164           #include <atf-c.h>
165
166           ATF_TC(tc1);
167           ATF_TC_HEAD(tc1, tc)
168           {
169               ... first test case's header ...
170           }
171           ATF_TC_BODY(tc1, tc)
172           {
173               ... first test case's body ...
174           }
175
176           ATF_TC_WITH_CLEANUP(tc2);
177           ATF_TC_HEAD(tc2, tc)
178           {
179               ... second test case's header ...
180           }
181           ATF_TC_BODY(tc2, tc)
182           {
183               ... second test case's body ...
184           }
185           ATF_TC_CLEANUP(tc2, tc)
186           {
187               ... second test case's cleanup ...
188           }
189
190           ATF_TC_WITHOUT_HEAD(tc3);
191           ATF_TC_BODY(tc3, tc)
192           {
193               ... third test case's body ...
194           }
195
196           ... additional test cases ...
197
198           ATF_TP_ADD_TCS(tp)
199           {
200               ATF_TP_ADD_TC(tcs, tc1);
201               ATF_TP_ADD_TC(tcs, tc2);
202               ATF_TP_ADD_TC(tcs, tc3);
203               ... add additional test cases ...
204
205               return atf_no_error();
206           }
207
208   Definition of test cases
209     Test cases have an identifier and are composed of three different parts:
210     the header, the body and an optional cleanup routine, all of which are
211     described in atf-test-case(4).  To define test cases, one can use the
212     ATF_TC(), ATF_TC_WITH_CLEANUP() or the ATF_TC_WITHOUT_HEAD() macros,
213     which take a single parameter specifiying the test case's name.
214     ATF_TC(), requires to define a head and a body for the test case,
215     ATF_TC_WITH_CLEANUP() requires to define a head, a body and a cleanup for
216     the test case and ATF_TC_WITHOUT_HEAD() requires only a body for the test
217     case.  It is important to note that these do not set the test case up for
218     execution when the program is run.  In order to do so, a later registra‐
219     tion is needed with the ATF_TP_ADD_TC() macro detailed in Program
220     initialization.
221
222     Later on, one must define the three parts of the body by means of three
223     functions.  Their headers are given by the ATF_TC_HEAD(), ATF_TC_BODY()
224     and ATF_TC_CLEANUP() macros, all of which take the test case name pro‐
225     vided to the ATF_TC() ATF_TC_WITH_CLEANUP(), or ATF_TC_WITHOUT_HEAD()
226     macros and the name of the variable that will hold a pointer to the test
227     case data.  Following each of these, a block of code is expected, sur‐
228     rounded by the opening and closing brackets.
229
230   Program initialization
231     The library provides a way to easily define the test program's main()
232     function.  You should never define one on your own, but rely on the
233     library to do it for you.  This is done by using the ATF_TP_ADD_TCS()
234     macro, which is passed the name of the object that will hold the test
235     cases; i.e. the test program instance.  This name can be whatever you
236     want as long as it is a valid variable identifier.
237
238     After the macro, you are supposed to provide the body of a function,
239     which should only use the ATF_TP_ADD_TC() macro to register the test
240     cases the test program will execute and return a success error code.  The
241     first parameter of this macro matches the name you provided in the former
242     call.  The success status can be returned using the atf_no_error() func‐
243     tion.
244
245   Header definitions
246     The test case's header can define the meta-data by using the
247     atf_tc_set_md_var() method, which takes three parameters: the first one
248     points to the test case data, the second one specifies the meta-data
249     variable to be set and the third one specifies its value.  Both of them
250     are strings.
251
252   Configuration variables
253     The test case has read-only access to the current configuration variables
254     by means of the bool atf_tc_has_config_var(), const char *
255     atf_tc_get_config_var(), const char * atf_tc_get_config_var_wd(), bool
256     atf_tc_get_config_var_as_bool(), bool atf_tc_get_config_var_as_bool_wd(),
257     long atf_tc_get_config_var_as_long(), and the long
258     atf_tc_get_config_var_as_long_wd() functions, which can be called in any
259     of the three parts of a test case.
260
261     The ‘_wd’ variants take a default value for the variable which is
262     returned if the variable is not defined.  The other functions without the
263     ‘_wd’ suffix require the variable to be defined.
264
265   Access to the source directory
266     It is possible to get the path to the test case's source directory from
267     any of its three components by querying the ‘srcdir’ configuration vari‐
268     able.
269
270   Requiring programs
271     Aside from the require.progs meta-data variable available in the header
272     only, one can also check for additional programs in the test case's body
273     by using the atf_tc_require_prog() function, which takes the base name or
274     full path of a single binary.  Relative paths are forbidden.  If it is
275     not found, the test case will be automatically skipped.
276
277   Test case finalization
278     The test case finalizes either when the body reaches its end, at which
279     point the test is assumed to have passed, unless any non-fatal errors
280     were raised using atf_tc_fail_nonfatal(), or at any explicit call to
281     atf_tc_pass(), atf_tc_fail() or atf_tc_skip().  These three functions
282     terminate the execution of the test case immediately.  The cleanup rou‐
283     tine will be processed afterwards in a completely automated way, regard‐
284     less of the test case's termination reason.
285
286     atf_tc_pass() does not take any parameters.  atf_tc_fail(),
287     atf_tc_fail_nonfatal() and atf_tc_skip() take a format string and a vari‐
288     able list of parameters, which describe, in a user-friendly manner, why
289     the test case failed or was skipped, respectively.  It is very important
290     to provide a clear error message in both cases so that the user can
291     quickly know why the test did not pass.
292
293   Expectations
294     Everything explained in the previous section changes when the test case
295     expectations are redefined by the programmer.
296
297     Each test case has an internal state called ‘expect’ that describes what
298     the test case expectations are at any point in time.  The value of this
299     property can change during execution by any of:
300
301     atf_tc_expect_death(reason, ...)
302             Expects the test case to exit prematurely regardless of the
303             nature of the exit.
304
305     atf_tc_expect_exit(exitcode, reason, ...)
306             Expects the test case to exit cleanly.  If exitcode is not ‘-1’,
307             atf-run(1) will validate that the exit code of the test case
308             matches the one provided in this call.  Otherwise, the exact
309             value will be ignored.
310
311     atf_tc_expect_fail(reason, ...)
312             Any failure (be it fatal or non-fatal) raised in this mode is
313             recorded.  However, such failures do not report the test case as
314             failed; instead, the test case finalizes cleanly and is reported
315             as ‘expected failure’; this report includes the provided reason
316             as part of it.  If no error is raised while running in this mode,
317             then the test case is reported as ‘failed’.
318
319             This mode is useful to reproduce actual known bugs in tests.
320             Whenever the developer fixes the bug later on, the test case will
321             start reporting a failure, signaling the developer that the test
322             case must be adjusted to the new conditions.  In this situation,
323             it is useful, for example, to set reason as the bug number for
324             tracking purposes.
325
326     atf_tc_expect_pass()
327             This is the normal mode of execution.  In this mode, any failure
328             is reported as such to the user and the test case is marked as
329             ‘failed’.
330
331     atf_tc_expect_signal(signo, reason, ...)
332             Expects the test case to terminate due to the reception of a sig‐
333             nal.  If signo is not ‘-1’, atf-run(1) will validate that the
334             signal that terminated the test case matches the one provided in
335             this call.  Otherwise, the exact value will be ignored.
336
337     atf_tc_expect_timeout(reason, ...)
338             Expects the test case to execute for longer than its timeout.
339
340   Helper macros for common checks
341     The library provides several macros that are very handy in multiple situ‐
342     ations.  These basically check some condition after executing a given
343     statement or processing a given expression and, if the condition is not
344     met, they report the test case as failed.
345
346     The ‘REQUIRE’ variant of the macros immediately abort the test case as
347     soon as an error condition is detected by calling the atf_tc_fail() func‐
348     tion.  Use this variant whenever it makes no sense to continue the execu‐
349     tion of a test case when the checked condition is not met.  The ‘CHECK’
350     variant, on the other hand, reports a failure as soon as it is encoun‐
351     tered using the atf_tc_fail_nonfatal() function, but the execution of the
352     test case continues as if nothing had happened.  Use this variant when‐
353     ever the checked condition is important as a result of the test case, but
354     there are other conditions that can be subsequently checked on the same
355     run without aborting.
356
357     Additionally, the ‘MSG’ variants take an extra set of parameters to
358     explicitly specify the failure message.  This failure message is format‐
359     ted according to the printf(3) formatters.
360
361     ATF_CHECK(), ATF_CHECK_MSG(), ATF_REQUIRE() and ATF_REQUIRE_MSG() take an
362     expression and fail if the expression evaluates to false.
363
364     ATF_CHECK_EQ(), ATF_CHECK_EQ_MSG(), ATF_REQUIRE_EQ() and
365     ATF_REQUIRE_EQ_MSG() take two expressions and fail if the two evaluated
366     values are not equal.
367
368     ATF_CHECK_MATCH(), ATF_CHECK_MATCH_MSG(), ATF_REQUIRE_MATCH() and
369     ATF_REQUIRE_MATCH_MSG() take a regular expression and a string and fail
370     if the regular expression does not match the given string.  Note that the
371     regular expression is not anchored, so it will match anywhere in the
372     string.
373
374     ATF_CHECK_STREQ(), ATF_CHECK_STREQ_MSG(), ATF_REQUIRE_STREQ() and
375     ATF_REQUIRE_STREQ_MSG() take two strings and fail if the two are not
376     equal character by character.
377
378     ATF_CHECK_ERRNO() and ATF_REQUIRE_ERRNO() take, first, the error code
379     that the check is expecting to find in the errno variable and, second, a
380     boolean expression that, if evaluates to true, means that a call failed
381     and errno has to be checked against the first value.
382
383   Utility functions
384     The following functions are provided as part of the atf-c-api API to sim‐
385     plify the creation of a variety of tests.  In particular, these are use‐
386     ful to write tests for command-line interfaces.
387
388     void atf_utils_cat_file(const char *file, const char *prefix)
389
390           Prints the contents of file to the standard output, prefixing every
391           line with the string in prefix.
392
393     bool atf_utils_compare_file(const char *file, const char *contents)
394
395           Returns true if the given file matches exactly the expected inlined
396           contents.
397
398     void atf_utils_copy_file(const char *source, const char *destination)
399
400           Copies the file source to destination.  The permissions of the file
401           are preserved during the code.
402
403     void atf_utils_create_file(const char *file, const char *contents, ...)
404
405           Creates file with the text given in contents, which is a formatting
406           string that uses the rest of the variable arguments.
407
408     void atf_utils_file_exists(const char *file)
409
410           Checks if file exists.
411
412     pid_t atf_utils_fork(void)
413
414           Forks a process and redirects the standard output and standard
415           error of the child to files for later validation with
416           atf_utils_wait().  Fails the test case if the fork fails, so this
417           does not return an error.
418
419     void atf_utils_free_charpp(char **argv)
420
421           Frees a dynamically-allocated array of dynamically-allocated
422           strings.
423
424     bool atf_utils_grep_file(const char *regexp, const char *file, ...)
425
426           Searches for the regexp, which is a formatting string representing
427           the regular expression, in the file.  The variable arguments are
428           used to construct the regular expression.
429
430     bool atf_utils_grep_string(const char *regexp, const char *str, ...)
431
432           Searches for the regexp, which is a formatting string representing
433           the regular expression, in the literal string str.  The variable
434           arguments are used to construct the regular expression.
435
436     char * atf_utils_readline(int fd)
437
438           Reads a line from the file descriptor fd.  The line, if any, is
439           returned as a dynamically-allocated buffer that must be released
440           with free(3).  If there was nothing to read, returns ‘NULL’.
441
442     void atf_utils_redirect(const int fd, const char *file)
443
444           Redirects the given file descriptor fd to file.  This function
445           exits the process in case of an error and does not properly mark
446           the test case as failed.  As a result, it should only be used in
447           subprocesses of the test case; specially those spawned by
448           atf_utils_fork().
449
450     void atf_utils_wait(const pid_t pid, const int expected_exit_status,
451     const char *expected_stdout, const char *expected_stderr)
452
453           Waits and validates the result of a subprocess spawned with
454           atf_utils_wait().  The validation involves checking that the sub‐
455           process exited cleanly and returned the code specified in
456           expected_exit_status and that its standard output and standard
457           error match the strings given in expected_stdout and
458           expected_stderr.
459
460           If any of the expected_stdout or expected_stderr strings are pre‐
461           fixed with ‘save:’, then they specify the name of the file into
462           which to store the stdout or stderr of the subprocess, and no com‐
463           parison is performed.
464

EXAMPLES

466     The following shows a complete test program with a single test case that
467     validates the addition operator:
468
469           #include <atf-c.h>
470
471           ATF_TC(addition);
472           ATF_TC_HEAD(addition, tc)
473           {
474               atf_tc_set_md_var(tc, "descr",
475                                 "Sample tests for the addition operator");
476           }
477           ATF_TC_BODY(addition, tc)
478           {
479               ATF_CHECK_EQ(0 + 0, 0);
480               ATF_CHECK_EQ(0 + 1, 1);
481               ATF_CHECK_EQ(1 + 0, 1);
482
483               ATF_CHECK_EQ(1 + 1, 2);
484
485               ATF_CHECK_EQ(100 + 200, 300);
486           }
487
488           ATF_TC(string_formatting);
489           ATF_TC_HEAD(string_formatting, tc)
490           {
491               atf_tc_set_md_var(tc, "descr",
492                                 "Sample tests for the snprintf");
493           }
494           ATF_TC_BODY(string_formatting, tc)
495           {
496               char buf[1024];
497               snprintf(buf, sizeof(buf), "a %s", "string");
498               ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
499           }
500
501           ATF_TC(open_failure);
502           ATF_TC_HEAD(open_failure, tc)
503           {
504               atf_tc_set_md_var(tc, "descr",
505                                 "Sample tests for the open function");
506           }
507           ATF_TC_BODY(open_failure, tc)
508           {
509               ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
510           }
511
512           ATF_TC(known_bug);
513           ATF_TC_HEAD(known_bug, tc)
514           {
515               atf_tc_set_md_var(tc, "descr",
516                                 "Reproduces a known bug");
517           }
518           ATF_TC_BODY(known_bug, tc)
519           {
520               atf_tc_expect_fail("See bug number foo/bar");
521               ATF_CHECK_EQ(3, 1 + 1);
522               atf_tc_expect_pass();
523               ATF_CHECK_EQ(3, 1 + 2);
524           }
525
526           ATF_TP_ADD_TCS(tp)
527           {
528               ATF_TP_ADD_TC(tp, addition);
529               ATF_TP_ADD_TC(tp, string_formatting);
530               ATF_TP_ADD_TC(tp, open_failure);
531               ATF_TP_ADD_TC(tp, known_bug);
532
533               return atf_no_error();
534           }
535

SEE ALSO

537     atf-test-program(1), atf-test-case(4), atf(7)
538
539BSD                            November 15, 2013                           BSD
Impressum