2 .\" Automated Testing Framework (atf)
4 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
16 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 .Nm ATF_CHECK_EQ_MSG ,
38 .Nm ATF_CHECK_STREQ_MSG ,
43 .Nm ATF_REQUIRE_EQ_MSG ,
44 .Nm ATF_REQUIRE_STREQ ,
45 .Nm ATF_REQUIRE_STREQ_MSG ,
46 .Nm ATF_REQUIRE_ERRNO ,
49 .Nm ATF_TC_BODY_NAME ,
51 .Nm ATF_TC_CLEANUP_NAME ,
53 .Nm ATF_TC_HEAD_NAME ,
55 .Nm ATF_TC_WITH_CLEANUP ,
56 .Nm ATF_TC_WITHOUT_HEAD ,
59 .Nm atf_tc_get_config_var ,
60 .Nm atf_tc_get_config_var_wd ,
61 .Nm atf_tc_get_config_var_as_bool ,
62 .Nm atf_tc_get_config_var_as_bool_wd ,
63 .Nm atf_tc_get_config_var_as_long ,
64 .Nm atf_tc_get_config_var_as_long_wd ,
66 .Nm atf_tc_expect_death ,
67 .Nm atf_tc_expect_exit ,
68 .Nm atf_tc_expect_fail ,
69 .Nm atf_tc_expect_pass ,
70 .Nm atf_tc_expect_signal ,
71 .Nm atf_tc_expect_timeout ,
73 .Nm atf_tc_fail_nonfatal ,
76 .Nd C API to write ATF-based test programs
79 .Fn ATF_CHECK "expression"
80 .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
81 .Fn ATF_CHECK_EQ "expression_1" "expression_2"
82 .Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
83 .Fn ATF_CHECK_STREQ "string_1" "string_2"
84 .Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
85 .Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression"
86 .Fn ATF_REQUIRE "expression"
87 .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
88 .Fn ATF_REQUIRE_EQ "expression_1" "expression_2"
89 .Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
90 .Fn ATF_REQUIRE_STREQ "string_1" "string_2"
91 .Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
92 .Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression"
94 .Fn ATF_TC_BODY "name" "tc"
95 .Fn ATF_TC_BODY_NAME "name"
96 .Fn ATF_TC_CLEANUP "name" "tc"
97 .Fn ATF_TC_CLEANUP_NAME "name"
98 .Fn ATF_TC_HEAD "name" "tc"
99 .Fn ATF_TC_HEAD_NAME "name"
100 .Fn ATF_TC_NAME "name"
101 .Fn ATF_TC_WITH_CLEANUP "name"
102 .Fn ATF_TC_WITHOUT_HEAD "name"
103 .Fn ATF_TP_ADD_TC "tp_name" "tc_name"
104 .Fn ATF_TP_ADD_TCS "tp_name"
105 .Fn atf_tc_get_config_var "tc" "varname"
106 .Fn atf_tc_get_config_var_wd "tc" "variable_name" "default_value"
107 .Fn atf_tc_get_config_var_as_bool "tc" "variable_name"
108 .Fn atf_tc_get_config_var_as_bool_wd "tc" "variable_name" "default_value"
109 .Fn atf_tc_get_config_var_as_long "tc" "variable_name"
110 .Fn atf_tc_get_config_var_as_long_wd "tc" "variable_name" "default_value"
112 .Fn atf_tc_expect_death "reason" "..."
113 .Fn atf_tc_expect_exit "exitcode" "reason" "..."
114 .Fn atf_tc_expect_fail "reason" "..."
115 .Fn atf_tc_expect_pass
116 .Fn atf_tc_expect_signal "signo" "reason" "..."
117 .Fn atf_tc_expect_timeout "reason" "..."
118 .Fn atf_tc_fail "reason"
119 .Fn atf_tc_fail_nonfatal "reason"
121 .Fn atf_tc_skip "reason"
125 C-based test programs always follow this template:
126 .Bd -literal -offset indent
127 .Ns ... C-specific includes go here ...
134 ... first test case's header ...
138 ... first test case's body ...
141 ATF_TC_WITH_CLEANUP(tc2);
144 ... second test case's header ...
148 ... second test case's body ...
150 ATF_TC_CLEANUP(tc2, tc)
152 ... second test case's cleanup ...
155 ATF_TC_WITHOUT_HEAD(tc3);
158 ... third test case's body ...
161 .Ns ... additional test cases ...
165 ATF_TP_ADD_TC(tcs, tc1);
166 ATF_TP_ADD_TC(tcs, tc2);
167 ATF_TP_ADD_TC(tcs, tc3);
168 ... add additional test cases ...
170 return atf_no_error();
173 .Ss Definition of test cases
174 Test cases have an identifier and are composed of three different parts:
175 the header, the body and an optional cleanup routine, all of which are
177 .Xr atf-test-case 4 .
178 To define test cases, one can use the
180 .Fn ATF_TC_WITH_CLEANUP
182 .Fn ATF_TC_WITHOUT_HEAD
183 macros, which take a single parameter specifiying the test case's name.
185 requires to define a head and a body for the test case,
186 .Fn ATF_TC_WITH_CLEANUP
187 requires to define a head, a body and a cleanup for the test case and
188 .Fn ATF_TC_WITHOUT_HEAD
189 requires only a body for the test case.
190 It is important to note that these
192 set the test case up for execution when the program is run.
193 In order to do so, a later registration is needed with the
196 .Sx Program initialization .
198 Later on, one must define the three parts of the body by means of three
200 Their headers are given by the
205 macros, all of which take the test case name provided to the
207 .Fn ATF_TC_WITH_CLEANUP ,
209 .Fn ATF_TC_WITHOUT_HEAD
210 macros and the name of the variable that will hold a pointer to the
212 Following each of these, a block of code is expected, surrounded by the
213 opening and closing brackets.
214 .Ss Program initialization
215 The library provides a way to easily define the test program's
218 You should never define one on your own, but rely on the
219 library to do it for you.
220 This is done by using the
222 macro, which is passed the name of the object that will hold the test
223 cases; i.e. the test program instance.
224 This name can be whatever you want as long as it is a valid variable
227 After the macro, you are supposed to provide the body of a function, which
230 macro to register the test cases the test program will execute and return
231 a success error code.
232 The first parameter of this macro matches the name you provided in the
234 The success status can be returned using the
237 .Ss Header definitions
238 The test case's header can define the meta-data by using the
239 .Fn atf_tc_set_md_var
240 method, which takes three parameters: the first one points to the test
241 case data, the second one specifies the meta-data variable to be set
242 and the third one specifies its value.
243 Both of them are strings.
244 .Ss Configuration variables
245 The test case has read-only access to the current configuration variables
248 .Fn atf_tc_has_config_var ,
250 .Fn atf_tc_get_config_var ,
252 .Fn atf_tc_get_config_var_wd ,
254 .Fn atf_tc_get_config_var_as_bool ,
256 .Fn atf_tc_get_config_var_as_bool_wd ,
258 .Fn atf_tc_get_config_var_as_long ,
261 .Fn atf_tc_get_config_var_as_long_wd
262 functions, which can be called in any of the three parts of a test case.
266 variants take a default value for the variable which is returned if the
267 variable is not defined.
268 The other functions without the
272 the variable to be defined.
273 .Ss Access to the source directory
274 It is possible to get the path to the test case's source directory from any
275 of its three components by querying the
277 configuration variable.
278 .Ss Requiring programs
281 meta-data variable available in the header only, one can also check for
282 additional programs in the test case's body by using the
283 .Fn atf_tc_require_prog
284 function, which takes the base name or full path of a single binary.
285 Relative paths are forbidden.
286 If it is not found, the test case will be automatically skipped.
287 .Ss Test case finalization
288 The test case finalizes either when the body reaches its end, at which
289 point the test is assumed to have
291 unless any non-fatal errors were raised using
292 .Fn atf_tc_fail_nonfatal ,
293 or at any explicit call to
298 These three functions terminate the execution of the test case immediately.
299 The cleanup routine will be processed afterwards in a completely automated
300 way, regardless of the test case's termination reason.
303 does not take any parameters.
305 .Fn atf_tc_fail_nonfatal
308 take a format string and a variable list of parameters, which describe, in
309 a user-friendly manner, why the test case failed or was skipped,
311 It is very important to provide a clear error message in both cases so that
312 the user can quickly know why the test did not pass.
314 Everything explained in the previous section changes when the test case
315 expectations are redefined by the programmer.
317 Each test case has an internal state called
319 that describes what the test case expectations are at any point in time.
320 The value of this property can change during execution by any of:
321 .Bl -tag -width indent
322 .It Fn atf_tc_expect_death "reason" "..."
323 Expects the test case to exit prematurely regardless of the nature of the
325 .It Fn atf_tc_expect_exit "exitcode" "reason" "..."
326 Expects the test case to exit cleanly.
332 will validate that the exit code of the test case matches the one provided
334 Otherwise, the exact value will be ignored.
335 .It Fn atf_tc_expect_fail "reason" "..."
336 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
337 However, such failures do not report the test case as failed; instead, the
338 test case finalizes cleanly and is reported as
339 .Sq expected failure ;
340 this report includes the provided
343 If no error is raised while running in this mode, then the test case is
347 This mode is useful to reproduce actual known bugs in tests.
348 Whenever the developer fixes the bug later on, the test case will start
349 reporting a failure, signaling the developer that the test case must be
350 adjusted to the new conditions.
351 In this situation, it is useful, for example, to set
353 as the bug number for tracking purposes.
354 .It Fn atf_tc_expect_pass
355 This is the normal mode of execution.
356 In this mode, any failure is reported as such to the user and the test case
359 .It Fn atf_tc_expect_signal "signo" "reason" "..."
360 Expects the test case to terminate due to the reception of a signal.
366 will validate that the signal that terminated the test case matches the one
367 provided in this call.
368 Otherwise, the exact value will be ignored.
369 .It Fn atf_tc_expect_timeout "reason" "..."
370 Expects the test case to execute for longer than its timeout.
372 .Ss Helper macros for common checks
373 The library provides several macros that are very handy in multiple
375 These basically check some condition after executing a given statement or
376 processing a given expression and, if the condition is not met, they
377 report the test case as failed.
381 variant of the macros immediately abort the test case as soon as an error
382 condition is detected by calling the
385 Use this variant whenever it makes now sense to continue the execution of a
386 test case when the checked condition is not met.
389 variant, on the other hand, reports a failure as soon as it is encountered
391 .Fn atf_tc_fail_nonfatal
392 function, but the execution of the test case continues as if nothing had
394 Use this variant whenever the checked condition is important as a result of
395 the test case, but there are other conditions that can be subsequently
396 checked on the same run without aborting.
400 variants take an extra set of parameters to explicitly specify the failure
402 This failure message is formatted according to the
411 take an expression and fail if the expression evaluates to false.
414 .Fn ATF_CHECK_EQ_MSG ,
417 .Fn ATF_REQUIRE_EQ_MSG
418 take two expressions and fail if the two evaluated values are not equal.
420 .Fn ATF_CHECK_STREQ ,
421 .Fn ATF_CHECK_STREQ_MSG ,
422 .Fn ATF_REQUIRE_STREQ
424 .Fn ATF_REQUIRE_STREQ_MSG
425 take two strings and fail if the two are not equal character by character.
429 .Fn ATF_REQUIRE_ERRNO
430 take, first, the error code that the check is expecting to find in the
432 variable and, second, a boolean expression that, if evaluates to true,
433 means that a call failed and
435 has to be checked against the first value.
437 The following shows a complete test program with a single test case that
438 validates the addition operator:
439 .Bd -literal -offset indent
443 ATF_TC_HEAD(addition, tc)
445 atf_tc_set_md_var(tc, "descr",
446 "Sample tests for the addition operator");
448 ATF_TC_BODY(addition, tc)
450 ATF_CHECK_EQ(0 + 0, 0);
451 ATF_CHECK_EQ(0 + 1, 1);
452 ATF_CHECK_EQ(1 + 0, 1);
454 ATF_CHECK_EQ(1 + 1, 2);
456 ATF_CHECK_EQ(100 + 200, 300);
459 ATF_TC(string_formatting);
460 ATF_TC_HEAD(string_formatting, tc)
462 atf_tc_set_md_var(tc, "descr",
463 "Sample tests for the snprintf");
465 ATF_TC_BODY(string_formatting, tc)
468 snprintf(buf, sizeof(buf), "a %s", "string");
469 ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
472 ATF_TC(open_failure);
473 ATF_TC_HEAD(open_failure, tc)
475 atf_tc_set_md_var(tc, "descr",
476 "Sample tests for the open function");
478 ATF_TC_BODY(open_failure, tc)
480 ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
484 ATF_TC_HEAD(known_bug, tc)
486 atf_tc_set_md_var(tc, "descr",
487 "Reproduces a known bug");
489 ATF_TC_BODY(known_bug, tc)
491 atf_tc_expect_fail("See bug number foo/bar");
492 ATF_CHECK_EQ(3, 1 + 1);
493 atf_tc_expect_pass();
494 ATF_CHECK_EQ(3, 1 + 2);
499 ATF_TP_ADD_TC(tp, addition);
500 ATF_TP_ADD_TC(tp, string_formatting);
501 ATF_TP_ADD_TC(tp, open_failure);
502 ATF_TP_ADD_TC(tp, known_bug);
504 return atf_no_error();
508 .Xr atf-test-program 1 ,
509 .Xr atf-test-case 4 ,