1 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 .Nm ATF_CHECK_EQ_MSG ,
35 .Nm ATF_CHECK_MATCH_MSG ,
37 .Nm ATF_CHECK_STREQ_MSG ,
42 .Nm ATF_REQUIRE_EQ_MSG ,
43 .Nm ATF_REQUIRE_MATCH ,
44 .Nm ATF_REQUIRE_MATCH_MSG ,
45 .Nm ATF_REQUIRE_STREQ ,
46 .Nm ATF_REQUIRE_STREQ_MSG ,
47 .Nm ATF_REQUIRE_ERRNO ,
50 .Nm ATF_TC_BODY_NAME ,
52 .Nm ATF_TC_CLEANUP_NAME ,
54 .Nm ATF_TC_HEAD_NAME ,
56 .Nm ATF_TC_WITH_CLEANUP ,
57 .Nm ATF_TC_WITHOUT_HEAD ,
60 .Nm atf_tc_get_config_var ,
61 .Nm atf_tc_get_config_var_wd ,
62 .Nm atf_tc_get_config_var_as_bool ,
63 .Nm atf_tc_get_config_var_as_bool_wd ,
64 .Nm atf_tc_get_config_var_as_long ,
65 .Nm atf_tc_get_config_var_as_long_wd ,
67 .Nm atf_tc_expect_death ,
68 .Nm atf_tc_expect_exit ,
69 .Nm atf_tc_expect_fail ,
70 .Nm atf_tc_expect_pass ,
71 .Nm atf_tc_expect_signal ,
72 .Nm atf_tc_expect_timeout ,
74 .Nm atf_tc_fail_nonfatal ,
77 .Nm atf_utils_cat_file ,
78 .Nm atf_utils_compare_file ,
79 .Nm atf_utils_copy_file ,
80 .Nm atf_utils_create_file ,
81 .Nm atf_utils_file_exists ,
83 .Nm atf_utils_free_charpp ,
84 .Nm atf_utils_grep_file ,
85 .Nm atf_utils_grep_string ,
86 .Nm atf_utils_readline ,
87 .Nm atf_utils_redirect ,
89 .Nd C API to write ATF-based test programs
92 .\" NO_CHECK_STYLE_BEGIN
93 .Fn ATF_CHECK "expression"
94 .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
95 .Fn ATF_CHECK_EQ "expected_expression" "actual_expression"
96 .Fn ATF_CHECK_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ...
97 .Fn ATF_CHECK_MATCH "regexp" "string"
98 .Fn ATF_CHECK_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
99 .Fn ATF_CHECK_STREQ "string_1" "string_2"
100 .Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
101 .Fn ATF_CHECK_ERRNO "expected_errno" "bool_expression"
102 .Fn ATF_REQUIRE "expression"
103 .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
104 .Fn ATF_REQUIRE_EQ "expected_expression" "actual_expression"
105 .Fn ATF_REQUIRE_EQ_MSG "expected_expression" "actual_expression" "fail_msg_fmt" ...
106 .Fn ATF_REQUIRE_MATCH "regexp" "string"
107 .Fn ATF_REQUIRE_MATCH_MSG "regexp" "string" "fail_msg_fmt" ...
108 .Fn ATF_REQUIRE_STREQ "expected_string" "actual_string"
109 .Fn ATF_REQUIRE_STREQ_MSG "expected_string" "actual_string" "fail_msg_fmt" ...
110 .Fn ATF_REQUIRE_ERRNO "expected_errno" "bool_expression"
111 .\" NO_CHECK_STYLE_END
113 .Fn ATF_TC_BODY "name" "tc"
114 .Fn ATF_TC_BODY_NAME "name"
115 .Fn ATF_TC_CLEANUP "name" "tc"
116 .Fn ATF_TC_CLEANUP_NAME "name"
117 .Fn ATF_TC_HEAD "name" "tc"
118 .Fn ATF_TC_HEAD_NAME "name"
119 .Fn ATF_TC_NAME "name"
120 .Fn ATF_TC_WITH_CLEANUP "name"
121 .Fn ATF_TC_WITHOUT_HEAD "name"
122 .Fn ATF_TP_ADD_TC "tp_name" "tc_name"
123 .Fn ATF_TP_ADD_TCS "tp_name"
124 .Fn atf_tc_get_config_var "tc" "varname"
125 .Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value"
126 .Fn atf_tc_get_config_var_as_bool "tc" "variable_name"
127 .Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value"
128 .Fn atf_tc_get_config_var_as_long "tc" "variable_name"
129 .Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value"
131 .Fn atf_tc_expect_death "reason" "..."
132 .Fn atf_tc_expect_exit "exitcode" "reason" "..."
133 .Fn atf_tc_expect_fail "reason" "..."
134 .Fn atf_tc_expect_pass
135 .Fn atf_tc_expect_signal "signo" "reason" "..."
136 .Fn atf_tc_expect_timeout "reason" "..."
137 .Fn atf_tc_fail "reason"
138 .Fn atf_tc_fail_nonfatal "reason"
140 .Fn atf_tc_skip "reason"
142 .Fo atf_utils_cat_file
143 .Fa "const char *file"
144 .Fa "const char *prefix"
147 .Fo atf_utils_compare_file
148 .Fa "const char *file"
149 .Fa "const char *contents"
152 .Fo atf_utils_copy_file
153 .Fa "const char *source"
154 .Fa "const char *destination"
157 .Fo atf_utils_create_file
158 .Fa "const char *file"
159 .Fa "const char *contents"
163 .Fo atf_utils_file_exists
164 .Fa "const char *file"
171 .Fo atf_utils_free_charpp
175 .Fo atf_utils_grep_file
176 .Fa "const char *regexp"
177 .Fa "const char *file"
181 .Fo atf_utils_grep_string
182 .Fa "const char *regexp"
183 .Fa "const char *str"
187 .Fo atf_utils_readline
191 .Fo atf_utils_redirect
193 .Fa "const char *file"
197 .Fa "const pid_t pid"
198 .Fa "const int expected_exit_status"
199 .Fa "const char *expected_stdout"
200 .Fa "const char *expected_stderr"
203 ATF provides a C programming interface to implement test programs.
204 C-based test programs follow this template:
205 .Bd -literal -offset indent
206 \&... C-specific includes go here ...
213 ... first test case's header ...
217 ... first test case's body ...
220 ATF_TC_WITH_CLEANUP(tc2);
223 ... second test case's header ...
227 ... second test case's body ...
229 ATF_TC_CLEANUP(tc2, tc)
231 ... second test case's cleanup ...
234 ATF_TC_WITHOUT_HEAD(tc3);
237 ... third test case's body ...
240 \&... additional test cases ...
244 ATF_TP_ADD_TC(tcs, tc1);
245 ATF_TP_ADD_TC(tcs, tc2);
246 ATF_TP_ADD_TC(tcs, tc3);
247 ... add additional test cases ...
249 return atf_no_error();
252 .Ss Definition of test cases
253 Test cases have an identifier and are composed of three different parts:
254 the header, the body and an optional cleanup routine, all of which are
256 .Xr atf-test-case 4 .
257 To define test cases, one can use the
259 .Fn ATF_TC_WITH_CLEANUP
261 .Fn ATF_TC_WITHOUT_HEAD
262 macros, which take a single parameter specifying the test case's name.
264 requires to define a head and a body for the test case,
265 .Fn ATF_TC_WITH_CLEANUP
266 requires to define a head, a body and a cleanup for the test case and
267 .Fn ATF_TC_WITHOUT_HEAD
268 requires only a body for the test case.
269 It is important to note that these
271 set the test case up for execution when the program is run.
272 In order to do so, a later registration is needed with the
275 .Sx Program initialization .
277 Later on, one must define the three parts of the body by means of three
279 Their headers are given by the
284 macros, all of which take the test case name provided to the
286 .Fn ATF_TC_WITH_CLEANUP ,
288 .Fn ATF_TC_WITHOUT_HEAD
289 macros and the name of the variable that will hold a pointer to the
291 Following each of these, a block of code is expected, surrounded by the
292 opening and closing brackets.
293 .Ss Program initialization
294 The library provides a way to easily define the test program's
297 You should never define one on your own, but rely on the
298 library to do it for you.
299 This is done by using the
301 macro, which is passed the name of the object that will hold the test
302 cases, i.e., the test program instance.
303 This name can be whatever you want as long as it is a valid variable
306 After the macro, you are supposed to provide the body of a function, which
309 macro to register the test cases the test program will execute and return
310 a success error code.
311 The first parameter of this macro matches the name you provided in the
313 The success status can be returned using the
316 .Ss Header definitions
317 The test case's header can define the meta-data by using the
318 .Fn atf_tc_set_md_var
319 method, which takes three parameters: the first one points to the test
320 case data, the second one specifies the meta-data variable to be set
321 and the third one specifies its value.
322 Both of them are strings.
323 .Ss Configuration variables
324 The test case has read-only access to the current configuration variables
327 .Fn atf_tc_has_config_var ,
329 .Fn atf_tc_get_config_var ,
331 .Fn atf_tc_get_config_var_wd ,
333 .Fn atf_tc_get_config_var_as_bool ,
335 .Fn atf_tc_get_config_var_as_bool_wd ,
337 .Fn atf_tc_get_config_var_as_long ,
340 .Fn atf_tc_get_config_var_as_long_wd
341 functions, which can be called in any of the three parts of a test case.
345 variants take a default value for the variable which is returned if the
346 variable is not defined.
347 The other functions without the
351 the variable to be defined.
352 .Ss Access to the source directory
353 It is possible to get the path to the test case's source directory from any
354 of its three components by querying the
356 configuration variable.
357 .Ss Requiring programs
360 meta-data variable available in the header only, one can also check for
361 additional programs in the test case's body by using the
362 .Fn atf_tc_require_prog
363 function, which takes the base name or full path of a single binary.
364 Relative paths are forbidden.
365 If it is not found, the test case will be automatically skipped.
366 .Ss Test case finalization
367 The test case finalizes either when the body reaches its end, at which
368 point the test is assumed to have
370 unless any non-fatal errors were raised using
371 .Fn atf_tc_fail_nonfatal ,
372 or at any explicit call to
377 These three functions terminate the execution of the test case immediately.
378 The cleanup routine will be processed afterwards in a completely automated
379 way, regardless of the test case's termination reason.
382 does not take any parameters.
384 .Fn atf_tc_fail_nonfatal
387 take a format string and a variable list of parameters, which describe, in
388 a user-friendly manner, why the test case failed or was skipped,
390 It is very important to provide a clear error message in both cases so that
391 the user can quickly know why the test did not pass.
393 Everything explained in the previous section changes when the test case
394 expectations are redefined by the programmer.
396 Each test case has an internal state called
398 that describes what the test case expectations are at any point in time.
399 The value of this property can change during execution by any of:
400 .Bl -tag -width indent
401 .It Fn atf_tc_expect_death "reason" "..."
402 Expects the test case to exit prematurely regardless of the nature of the
404 .It Fn atf_tc_expect_exit "exitcode" "reason" "..."
405 Expects the test case to exit cleanly.
410 the runtime engine will validate that the exit code of the test case
411 matches the one provided in this call.
412 Otherwise, the exact value will be ignored.
413 .It Fn atf_tc_expect_fail "reason" "..."
414 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
415 However, such failures do not report the test case as failed; instead, the
416 test case finalizes cleanly and is reported as
417 .Sq expected failure ;
418 this report includes the provided
421 If no error is raised while running in this mode, then the test case is
425 This mode is useful to reproduce actual known bugs in tests.
426 Whenever the developer fixes the bug later on, the test case will start
427 reporting a failure, signaling the developer that the test case must be
428 adjusted to the new conditions.
429 In this situation, it is useful, for example, to set
431 as the bug number for tracking purposes.
432 .It Fn atf_tc_expect_pass
433 This is the normal mode of execution.
434 In this mode, any failure is reported as such to the user and the test case
437 .It Fn atf_tc_expect_signal "signo" "reason" "..."
438 Expects the test case to terminate due to the reception of a signal.
443 the runtime engine will validate that the signal that terminated the test
444 case matches the one provided in this call.
445 Otherwise, the exact value will be ignored.
446 .It Fn atf_tc_expect_timeout "reason" "..."
447 Expects the test case to execute for longer than its timeout.
449 .Ss Helper macros for common checks
450 The library provides several macros that are very handy in multiple
452 These basically check some condition after executing a given statement or
453 processing a given expression and, if the condition is not met, they
454 report the test case as failed.
458 variant of the macros immediately abort the test case as soon as an error
459 condition is detected by calling the
462 Use this variant whenever it makes no sense to continue the execution of a
463 test case when the checked condition is not met.
466 variant, on the other hand, reports a failure as soon as it is encountered
468 .Fn atf_tc_fail_nonfatal
469 function, but the execution of the test case continues as if nothing had
471 Use this variant whenever the checked condition is important as a result of
472 the test case, but there are other conditions that can be subsequently
473 checked on the same run without aborting.
477 variants take an extra set of parameters to explicitly specify the failure
479 This failure message is formatted according to the
488 take an expression and fail if the expression evaluates to false.
491 .Fn ATF_CHECK_EQ_MSG ,
494 .Fn ATF_REQUIRE_EQ_MSG
495 take two expressions and fail if the two evaluated values are not equal.
496 The common style is to put the expected value in the first parameter and the
497 actual value in the second parameter.
499 .Fn ATF_CHECK_MATCH ,
500 .Fn ATF_CHECK_MATCH_MSG ,
501 .Fn ATF_REQUIRE_MATCH
503 .Fn ATF_REQUIRE_MATCH_MSG
504 take a regular expression and a string and fail if the regular expression does
505 not match the given string.
506 Note that the regular expression is not anchored, so it will match anywhere in
509 .Fn ATF_CHECK_STREQ ,
510 .Fn ATF_CHECK_STREQ_MSG ,
511 .Fn ATF_REQUIRE_STREQ
513 .Fn ATF_REQUIRE_STREQ_MSG
514 take two strings and fail if the two are not equal character by character.
515 The common style is to put the expected string in the first parameter and the
516 actual string in the second parameter.
520 .Fn ATF_REQUIRE_ERRNO
521 take, first, the error code that the check is expecting to find in the
523 variable and, second, a boolean expression that, if evaluates to true,
524 means that a call failed and
526 has to be checked against the first value.
527 .Ss Utility functions
528 The following functions are provided as part of the
530 API to simplify the creation of a variety of tests.
531 In particular, these are useful to write tests for command-line interfaces.
534 .Fo atf_utils_cat_file
535 .Fa "const char *file"
536 .Fa "const char *prefix"
538 .Bd -ragged -offset indent
539 Prints the contents of
541 to the standard output, prefixing every line with the string in
546 .Fo atf_utils_compare_file
547 .Fa "const char *file"
548 .Fa "const char *contents"
550 .Bd -ragged -offset indent
551 Returns true if the given
553 matches exactly the expected inlined
558 .Fo atf_utils_copy_file
559 .Fa "const char *source"
560 .Fa "const char *destination"
562 .Bd -ragged -offset indent
567 The permissions of the file are preserved during the code.
571 .Fo atf_utils_create_file
572 .Fa "const char *file"
573 .Fa "const char *contents"
576 .Bd -ragged -offset indent
579 with the text given in
581 which is a formatting string that uses the rest of the variable arguments.
585 .Fo atf_utils_file_exists
586 .Fa "const char *file"
588 .Bd -ragged -offset indent
598 .Bd -ragged -offset indent
599 Forks a process and redirects the standard output and standard error of the
600 child to files for later validation with
602 Fails the test case if the fork fails, so this does not return an error.
606 .Fo atf_utils_free_charpp
609 .Bd -ragged -offset indent
610 Frees a dynamically-allocated array of dynamically-allocated strings.
614 .Fo atf_utils_grep_file
615 .Fa "const char *regexp"
616 .Fa "const char *file"
619 .Bd -ragged -offset indent
622 which is a formatting string representing the regular expression,
625 The variable arguments are used to construct the regular expression.
629 .Fo atf_utils_grep_string
630 .Fa "const char *regexp"
631 .Fa "const char *str"
634 .Bd -ragged -offset indent
637 which is a formatting string representing the regular expression,
638 in the literal string
640 The variable arguments are used to construct the regular expression.
644 .Fo atf_utils_readline
647 .Bd -ragged -offset indent
648 Reads a line from the file descriptor
650 The line, if any, is returned as a dynamically-allocated buffer that must be
653 If there was nothing to read, returns
658 .Fo atf_utils_redirect
660 .Fa "const char *file"
662 .Bd -ragged -offset indent
663 Redirects the given file descriptor
667 This function exits the process in case of an error and does not properly mark
668 the test case as failed.
669 As a result, it should only be used in subprocesses of the test case; specially
676 .Fa "const pid_t pid"
677 .Fa "const int expected_exit_status"
678 .Fa "const char *expected_stdout"
679 .Fa "const char *expected_stderr"
681 .Bd -ragged -offset indent
682 Waits and validates the result of a subprocess spawned with
684 The validation involves checking that the subprocess exited cleanly and returned
685 the code specified in
686 .Fa expected_exit_status
687 and that its standard output and standard error match the strings given in
690 .Fa expected_stderr .
696 strings are prefixed with
698 then they specify the name of the file into which to store the stdout or stderr
699 of the subprocess, and no comparison is performed.
702 The following variables are recognized by
704 but should not be overridden other than for testing purposes:
706 .Bl -tag -width ATFXBUILDXCXXFLAGSXX -compact
708 Path to the C compiler.
709 .It Va ATF_BUILD_CFLAGS
712 Path to the C/C++ preprocessor.
713 .It Va ATF_BUILD_CPPFLAGS
714 C/C++ preprocessor flags.
716 Path to the C++ compiler.
717 .It Va ATF_BUILD_CXXFLAGS
721 The following shows a complete test program with a single test case that
722 validates the addition operator:
723 .Bd -literal -offset indent
727 ATF_TC_HEAD(addition, tc)
729 atf_tc_set_md_var(tc, "descr",
730 "Sample tests for the addition operator");
732 ATF_TC_BODY(addition, tc)
734 ATF_CHECK_EQ(0, 0 + 0);
735 ATF_CHECK_EQ(1, 0 + 1);
736 ATF_CHECK_EQ(1, 1 + 0);
738 ATF_CHECK_EQ(2, 1 + 1);
740 ATF_CHECK_EQ(300, 100 + 200);
743 ATF_TC(string_formatting);
744 ATF_TC_HEAD(string_formatting, tc)
746 atf_tc_set_md_var(tc, "descr",
747 "Sample tests for the snprintf");
749 ATF_TC_BODY(string_formatting, tc)
752 snprintf(buf, sizeof(buf), "a %s", "string");
753 ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
756 ATF_TC(open_failure);
757 ATF_TC_HEAD(open_failure, tc)
759 atf_tc_set_md_var(tc, "descr",
760 "Sample tests for the open function");
762 ATF_TC_BODY(open_failure, tc)
764 ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
768 ATF_TC_HEAD(known_bug, tc)
770 atf_tc_set_md_var(tc, "descr",
771 "Reproduces a known bug");
773 ATF_TC_BODY(known_bug, tc)
775 atf_tc_expect_fail("See bug number foo/bar");
776 ATF_CHECK_EQ(3, 1 + 1);
777 atf_tc_expect_pass();
778 ATF_CHECK_EQ(3, 1 + 2);
783 ATF_TP_ADD_TC(tp, addition);
784 ATF_TP_ADD_TC(tp, string_formatting);
785 ATF_TP_ADD_TC(tp, open_failure);
786 ATF_TP_ADD_TC(tp, known_bug);
788 return atf_no_error();
792 .Xr atf-test-program 1 ,