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.
34 .Nm ATF_ADD_TEST_CASE ,
37 .Nm ATF_INIT_TEST_CASES ,
41 .Nm ATF_REQUIRE_ERRNO ,
43 .Nm ATF_REQUIRE_MATCH ,
44 .Nm ATF_REQUIRE_NOT_IN ,
45 .Nm ATF_REQUIRE_THROW ,
46 .Nm ATF_REQUIRE_THROW_RE ,
49 .Nm ATF_TEST_CASE_BODY ,
50 .Nm ATF_TEST_CASE_CLEANUP ,
51 .Nm ATF_TEST_CASE_HEAD ,
52 .Nm ATF_TEST_CASE_NAME ,
53 .Nm ATF_TEST_CASE_USE ,
54 .Nm ATF_TEST_CASE_WITH_CLEANUP ,
55 .Nm ATF_TEST_CASE_WITHOUT_HEAD ,
56 .Nm atf::utils::cat_file ,
57 .Nm atf::utils::compare_file ,
58 .Nm atf::utils::copy_file ,
59 .Nm atf::utils::create_file ,
60 .Nm atf::utils::file_exists ,
61 .Nm atf::utils::fork ,
62 .Nm atf::utils::grep_collection ,
63 .Nm atf::utils::grep_file ,
64 .Nm atf::utils::grep_string ,
65 .Nm atf::utils::redirect ,
67 .Nd C++ API to write ATF-based test programs
70 .Fn ATF_ADD_TEST_CASE "tcs" "name"
71 .Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression"
73 .Fn ATF_INIT_TEST_CASES "tcs"
75 .Fn ATF_REQUIRE "expression"
76 .Fn ATF_REQUIRE_EQ "expression_1" "expression_2"
77 .Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression"
78 .Fn ATF_REQUIRE_IN "element" "collection"
79 .Fn ATF_REQUIRE_MATCH "regexp" "string_expression"
80 .Fn ATF_REQUIRE_NOT_IN "element" "collection"
81 .Fn ATF_REQUIRE_THROW "expected_exception" "statement"
82 .Fn ATF_REQUIRE_THROW_RE "expected_exception" "regexp" "statement"
84 .Fn ATF_TEST_CASE "name"
85 .Fn ATF_TEST_CASE_BODY "name"
86 .Fn ATF_TEST_CASE_CLEANUP "name"
87 .Fn ATF_TEST_CASE_HEAD "name"
88 .Fn ATF_TEST_CASE_NAME "name"
89 .Fn ATF_TEST_CASE_USE "name"
90 .Fn ATF_TEST_CASE_WITH_CLEANUP "name"
91 .Fn ATF_TEST_CASE_WITHOUT_HEAD "name"
93 .Fo atf::utils::cat_file
94 .Fa "const std::string& path"
95 .Fa "const std::string& prefix"
98 .Fo atf::utils::compare_file
99 .Fa "const std::string& path"
100 .Fa "const std::string& contents"
103 .Fo atf::utils::copy_file
104 .Fa "const std::string& source"
105 .Fa "const std::string& destination"
108 .Fo atf::utils::create_file
109 .Fa "const std::string& path"
110 .Fa "const std::string& contents"
113 .Fo atf::utils::file_exists
114 .Fa "const std::string& path"
121 .Fo atf::utils::grep_collection
122 .Fa "const std::string& regexp"
123 .Fa "const Collection& collection"
126 .Fo atf::utils::grep_file
127 .Fa "const std::string& regexp"
128 .Fa "const std::string& path"
131 .Fo atf::utils::grep_string
132 .Fa "const std::string& regexp"
133 .Fa "const std::string& path"
136 .Fo atf::utils::redirect
138 .Fa "const std::string& path"
142 .Fa "const pid_t pid"
143 .Fa "const int expected_exit_status"
144 .Fa "const std::string& expected_stdout"
145 .Fa "const std::string& expected_stderr"
148 ATF provides a C++ programming interface to implement test programs.
149 C++-based test programs follow this template:
150 .Bd -literal -offset indent
152 .Ns ... C-specific includes go here ...
155 .Ns ... C++-specific includes go here ...
157 #include <atf-c++.hpp>
160 ATF_TEST_CASE_HEAD(tc1)
162 ... first test case's header ...
164 ATF_TEST_CASE_BODY(tc1)
166 ... first test case's body ...
169 ATF_TEST_CASE_WITH_CLEANUP(tc2);
170 ATF_TEST_CASE_HEAD(tc2)
172 ... second test case's header ...
174 ATF_TEST_CASE_BODY(tc2)
176 ... second test case's body ...
178 ATF_TEST_CASE_CLEANUP(tc2)
180 ... second test case's cleanup ...
184 ATF_TEST_CASE_BODY(tc3)
186 ... third test case's body ...
189 .Ns ... additional test cases ...
191 ATF_INIT_TEST_CASES(tcs)
193 ATF_ADD_TEST_CASE(tcs, tc1);
194 ATF_ADD_TEST_CASE(tcs, tc2);
195 ATF_ADD_TEST_CASE(tcs, tc3);
196 ... add additional test cases ...
199 .Ss Definition of test cases
200 Test cases have an identifier and are composed of three different parts:
201 the header, the body and an optional cleanup routine, all of which are
203 .Xr atf-test-case 4 .
204 To define test cases, one can use the
206 .Fn ATF_TEST_CASE_WITH_CLEANUP
208 .Fn ATF_TEST_CASE_WITHOUT_HEAD
209 macros, which take a single parameter specifiying the test case's
212 requires to define a head and a body for the test case,
213 .Fn ATF_TEST_CASE_WITH_CLEANUP
214 requires to define a head, a body and a cleanup for the test case and
215 .Fn ATF_TEST_CASE_WITHOUT_HEAD
216 requires only a body for the test case.
217 It is important to note that these
219 set the test case up for execution when the program is run.
220 In order to do so, a later registration is needed through the
221 .Fn ATF_ADD_TEST_CASE
223 .Sx Program initialization .
225 Later on, one must define the three parts of the body by means of three
227 Their headers are given by the
228 .Fn ATF_TEST_CASE_HEAD ,
229 .Fn ATF_TEST_CASE_BODY
231 .Fn ATF_TEST_CASE_CLEANUP
232 macros, all of which take the test case's name.
233 Following each of these, a block of code is expected, surrounded by the
234 opening and closing brackets.
237 .Fn ATF_TEST_CASE_NAME
238 macro can be used to obtain the name of the class corresponding to a
239 particular test case, as the name is internally manged by the library to
240 prevent clashes with other user identifiers.
242 .Fn ATF_TEST_CASE_USE
243 macro can be executed on a particular test case to mark it as "used" and
244 thus prevent compiler warnings regarding unused symbols.
246 .Em you should never have to use these macros during regular operation.
247 .Ss Program initialization
248 The library provides a way to easily define the test program's
251 You should never define one on your own, but rely on the
252 library to do it for you.
253 This is done by using the
254 .Fn ATF_INIT_TEST_CASES
255 macro, which is passed the name of the list that will hold the test cases.
256 This name can be whatever you want as long as it is a valid variable value.
258 After the macro, you are supposed to provide the body of a function, which
260 .Fn ATF_ADD_TEST_CASE
261 macro to register the test cases the test program will execute.
262 The first parameter of this macro matches the name you provided in the
264 .Ss Header definitions
265 The test case's header can define the meta-data by using the
267 method, which takes two parameters: the first one specifies the
268 meta-data variable to be set and the second one specifies its value.
269 Both of them are strings.
270 .Ss Configuration variables
271 The test case has read-only access to the current configuration variables
278 methods, which can be called in any of the three parts of a test case.
279 .Ss Access to the source directory
280 It is possible to get the path to the test case's source directory from any
281 of its three components by querying the
283 configuration variable.
284 .Ss Requiring programs
287 meta-data variable available in the header only, one can also check for
288 additional programs in the test case's body by using the
290 function, which takes the base name or full path of a single binary.
291 Relative paths are forbidden.
292 If it is not found, the test case will be automatically skipped.
293 .Ss Test case finalization
294 The test case finalizes either when the body reaches its end, at which
295 point the test is assumed to have
297 or at any explicit call to
302 These three macros terminate the execution of the test case immediately.
303 The cleanup routine will be processed afterwards in a completely automated
304 way, regardless of the test case's termination reason.
307 does not take any parameters.
311 take a single string that describes why the test case failed or
312 was skipped, respectively.
313 It is very important to provide a clear error message in both cases so that
314 the user can quickly know why the test did not pass.
316 Everything explained in the previous section changes when the test case
317 expectations are redefined by the programmer.
319 Each test case has an internal state called
321 that describes what the test case expectations are at any point in time.
322 The value of this property can change during execution by any of:
323 .Bl -tag -width indent
324 .It Fn expect_death "reason"
325 Expects the test case to exit prematurely regardless of the nature of the
327 .It Fn expect_exit "exitcode" "reason"
328 Expects the test case to exit cleanly.
333 the runtime engine will validate that the exit code of the test case
334 matches the one provided in this call.
335 Otherwise, the exact value will be ignored.
336 .It Fn expect_fail "reason"
337 Any failure (be it fatal or non-fatal) raised in this mode is recorded.
338 However, such failures do not report the test case as failed; instead, the
339 test case finalizes cleanly and is reported as
340 .Sq expected failure ;
341 this report includes the provided
344 If no error is raised while running in this mode, then the test case is
348 This mode is useful to reproduce actual known bugs in tests.
349 Whenever the developer fixes the bug later on, the test case will start
350 reporting a failure, signaling the developer that the test case must be
351 adjusted to the new conditions.
352 In this situation, it is useful, for example, to set
354 as the bug number for tracking purposes.
356 This is the normal mode of execution.
357 In this mode, any failure is reported as such to the user and the test case
360 .It Fn expect_race "reason"
361 Any failure or timeout during the execution of the test case will be
362 considered as if a race condition has been triggered and reported as such.
363 If no problems arise, the test will continue execution as usual.
364 .It Fn expect_signal "signo" "reason"
365 Expects the test case to terminate due to the reception of a signal.
370 the runtime engine will validate that the signal that terminated the test
371 case matches the one provided in this call.
372 Otherwise, the exact value will be ignored.
373 .It Fn expect_timeout "reason"
374 Expects the test case to execute for longer than its timeout.
376 .Ss Helper macros for common checks
377 The library provides several macros that are very handy in multiple
379 These basically check some condition after executing a given statement or
380 processing a given expression and, if the condition is not met, they
383 with an appropriate error message.
386 takes an expression and raises a failure if it evaluates to false.
389 takes two expressions and raises a failure if the two do not evaluate to
390 the same exact value.
393 takes an element and a collection and validates that the element is present in
396 .Fn ATF_REQUIRE_MATCH
397 takes a regular expression and a string and raises a failure if the regular
398 expression does not match the string.
400 .Fn ATF_REQUIRE_NOT_IN
401 takes an element and a collection and validates that the element is not present
404 .Fn ATF_REQUIRE_THROW
405 takes the name of an exception and a statement and raises a failure if
406 the statement does not throw the specified exception.
407 .Fn ATF_REQUIRE_THROW_RE
408 takes the name of an exception, a regular expresion and a statement and raises a
409 failure if the statement does not throw the specified exception and if the
410 message of the exception does not match the regular expression.
414 .Fn ATF_REQUIRE_ERRNO
415 take, first, the error code that the check is expecting to find in the
417 variable and, second, a boolean expression that, if evaluates to true,
418 means that a call failed and
420 has to be checked against the first value.
421 .Ss Utility functions
422 The following functions are provided as part of the
424 API to simplify the creation of a variety of tests.
425 In particular, these are useful to write tests for command-line interfaces.
428 .Fo atf::utils::cat_file
429 .Fa "const std::string& path"
430 .Fa "const std::string& prefix"
432 .Bd -ragged -offset indent
433 Prints the contents of
435 to the standard output, prefixing every line with the string in
440 .Fo atf::utils::compare_file
441 .Fa "const std::string& path"
442 .Fa "const std::string& contents"
444 .Bd -ragged -offset indent
445 Returns true if the given
447 matches exactly the expected inlined
452 .Fo atf::utils::copy_file
453 .Fa "const std::string& source"
454 .Fa "const std::string& destination"
456 .Bd -ragged -offset indent
461 The permissions of the file are preserved during the code.
465 .Fo atf::utils::create_file
466 .Fa "const std::string& path"
467 .Fa "const std::string& contents"
469 .Bd -ragged -offset indent
472 with the text given in
477 .Fo atf::utils::file_exists
478 .Fa "const std::string& path"
480 .Bd -ragged -offset indent
490 .Bd -ragged -offset indent
491 Forks a process and redirects the standard output and standard error of the
492 child to files for later validation with
493 .Fn atf::utils::wait .
494 Fails the test case if the fork fails, so this does not return an error.
498 .Fo atf::utils::grep_collection
499 .Fa "const std::string& regexp"
500 .Fa "const Collection& collection"
502 .Bd -ragged -offset indent
503 Searches for the regular expression
505 in any of the strings contained in the
507 This is a template that accepts any one-dimensional container of strings.
511 .Fo atf::utils::grep_file
512 .Fa "const std::string& regexp"
513 .Fa "const std::string& path"
515 .Bd -ragged -offset indent
516 Searches for the regular expression
520 The variable arguments are used to construct the regular expression.
524 .Fo atf::utils::grep_string
525 .Fa "const std::string& regexp"
526 .Fa "const std::string& str"
528 .Bd -ragged -offset indent
529 Searches for the regular expression
535 .Fo atf::utils::redirect
537 .Fa "const std::string& path"
539 .Bd -ragged -offset indent
540 Redirects the given file descriptor
544 This function exits the process in case of an error and does not properly mark
545 the test case as failed.
546 As a result, it should only be used in subprocesses of the test case; specially
548 .Fn atf::utils::fork .
553 .Fa "const pid_t pid"
554 .Fa "const int expected_exit_status"
555 .Fa "const std::string& expected_stdout"
556 .Fa "const std::string& expected_stderr"
558 .Bd -ragged -offset indent
559 Waits and validates the result of a subprocess spawned with
560 .Fn atf::utils::wait .
561 The validation involves checking that the subprocess exited cleanly and returned
562 the code specified in
563 .Fa expected_exit_status
564 and that its standard output and standard error match the strings given in
567 .Fa expected_stderr .
573 strings are prefixed with
575 then they specify the name of the file into which to store the stdout or stderr
576 of the subprocess, and no comparison is performed.
579 The following shows a complete test program with a single test case that
580 validates the addition operator:
581 .Bd -literal -offset indent
582 #include <atf-c++.hpp>
584 ATF_TEST_CASE(addition);
585 ATF_TEST_CASE_HEAD(addition)
587 set_md_var("descr", "Sample tests for the addition operator");
589 ATF_TEST_CASE_BODY(addition)
591 ATF_REQUIRE_EQ(0 + 0, 0);
592 ATF_REQUIRE_EQ(0 + 1, 1);
593 ATF_REQUIRE_EQ(1 + 0, 1);
595 ATF_REQUIRE_EQ(1 + 1, 2);
597 ATF_REQUIRE_EQ(100 + 200, 300);
600 ATF_TEST_CASE(open_failure);
601 ATF_TEST_CASE_HEAD(open_failure)
603 set_md_var("descr", "Sample tests for the open function");
605 ATF_TEST_CASE_BODY(open_failure)
607 ATF_REQUIRE_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
610 ATF_TEST_CASE(known_bug);
611 ATF_TEST_CASE_HEAD(known_bug)
613 set_md_var("descr", "Reproduces a known bug");
615 ATF_TEST_CASE_BODY(known_bug)
617 expect_fail("See bug number foo/bar");
618 ATF_REQUIRE_EQ(3, 1 + 1);
620 ATF_REQUIRE_EQ(3, 1 + 2);
623 ATF_INIT_TEST_CASES(tcs)
625 ATF_ADD_TEST_CASE(tcs, addition);
626 ATF_ADD_TEST_CASE(tcs, open_failure);
627 ATF_ADD_TEST_CASE(tcs, known_bug);
631 .Xr atf-test-program 1 ,