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

NAME

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

DESCRIPTION

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

ENVIRONMENT

470     The following variables are recognized by atf-c but should not be over‐
471     ridden other than for testing purposes:
472
473     ATF_BUILD_CC          Path to the C compiler.
474     ATF_BUILD_CFLAGS      C compiler flags.
475     ATF_BUILD_CPP         Path to the C/C++ preprocessor.
476     ATF_BUILD_CPPFLAGS    C/C++ preprocessor flags.
477     ATF_BUILD_CXX         Path to the C++ compiler.
478     ATF_BUILD_CXXFLAGS    C++ compiler flags.
479

EXAMPLES

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

SEE ALSO

552     atf-test-program(1), atf-test-case(4)
553
554BSD                            October 13, 2014                            BSD
Impressum