1ATF-C++(3) BSD Library Functions Manual ATF-C++(3)
2
4 atf-c++, ATF_ADD_TEST_CASE, ATF_CHECK_ERRNO, ATF_FAIL,
5 ATF_INIT_TEST_CASES, ATF_PASS, ATF_REQUIRE, ATF_REQUIRE_EQ,
6 ATF_REQUIRE_ERRNO, ATF_REQUIRE_IN, ATF_REQUIRE_MATCH, ATF_REQUIRE_NOT_IN,
7 ATF_REQUIRE_THROW, ATF_REQUIRE_THROW_RE, ATF_SKIP, ATF_TEST_CASE,
8 ATF_TEST_CASE_BODY, ATF_TEST_CASE_CLEANUP, ATF_TEST_CASE_HEAD,
9 ATF_TEST_CASE_NAME, ATF_TEST_CASE_USE, ATF_TEST_CASE_WITH_CLEANUP,
10 ATF_TEST_CASE_WITHOUT_HEAD, atf::utils::cat_file,
11 atf::utils::compare_file, atf::utils::copy_file, atf::utils::create_file,
12 atf::utils::file_exists, atf::utils::fork, atf::utils::grep_collection,
13 atf::utils::grep_file, atf::utils::grep_string, atf::utils::redirect,
14 atf::utils::wait — C++ API to write ATF-based test programs
15
17 #include <atf-c++.hpp>
18
19 ATF_ADD_TEST_CASE(tcs, name);
20
21 ATF_CHECK_ERRNO(expected_errno, bool_expression);
22
23 ATF_FAIL(reason);
24
25 ATF_INIT_TEST_CASES(tcs);
26
27 ATF_PASS();
28
29 ATF_REQUIRE(expression);
30
31 ATF_REQUIRE_EQ(expected_expression, actual_expression);
32
33 ATF_REQUIRE_ERRNO(expected_errno, bool_expression);
34
35 ATF_REQUIRE_IN(element, collection);
36
37 ATF_REQUIRE_MATCH(regexp, string_expression);
38
39 ATF_REQUIRE_NOT_IN(element, collection);
40
41 ATF_REQUIRE_THROW(expected_exception, statement);
42
43 ATF_REQUIRE_THROW_RE(expected_exception, regexp, statement);
44
45 ATF_SKIP(reason);
46
47 ATF_TEST_CASE(name);
48
49 ATF_TEST_CASE_BODY(name);
50
51 ATF_TEST_CASE_CLEANUP(name);
52
53 ATF_TEST_CASE_HEAD(name);
54
55 ATF_TEST_CASE_NAME(name);
56
57 ATF_TEST_CASE_USE(name);
58
59 ATF_TEST_CASE_WITH_CLEANUP(name);
60
61 ATF_TEST_CASE_WITHOUT_HEAD(name);
62
63 void
64 atf::utils::cat_file(const std::string& path, const std::string& prefix);
65
66 bool
67 atf::utils::compare_file(const std::string& path,
68 const std::string& contents);
69
70 void
71 atf::utils::copy_file(const std::string& source,
72 const std::string& destination);
73
74 void
75 atf::utils::create_file(const std::string& path,
76 const std::string& contents);
77
78 void
79 atf::utils::file_exists(const std::string& path);
80
81 pid_t
82 atf::utils::fork(void);
83
84 bool
85 atf::utils::grep_collection(const std::string& regexp,
86 const Collection& collection);
87
88 bool
89 atf::utils::grep_file(const std::string& regexp,
90 const std::string& path);
91
92 bool
93 atf::utils::grep_string(const std::string& regexp,
94 const std::string& path);
95
96 void
97 atf::utils::redirect(const int fd, const std::string& path);
98
99 void
100 atf::utils::wait(const pid_t pid, const int expected_exit_status,
101 const std::string& expected_stdout,
102 const std::string& expected_stderr);
103
105 ATF provides a C++ programming interface to implement test programs.
106 C++-based test programs follow this template:
107
108 extern "C" {
109 ... C-specific includes go here ...
110 }
111
112 ... C++-specific includes go here ...
113
114 #include <atf-c++.hpp>
115
116 ATF_TEST_CASE(tc1);
117 ATF_TEST_CASE_HEAD(tc1)
118 {
119 ... first test case's header ...
120 }
121 ATF_TEST_CASE_BODY(tc1)
122 {
123 ... first test case's body ...
124 }
125
126 ATF_TEST_CASE_WITH_CLEANUP(tc2);
127 ATF_TEST_CASE_HEAD(tc2)
128 {
129 ... second test case's header ...
130 }
131 ATF_TEST_CASE_BODY(tc2)
132 {
133 ... second test case's body ...
134 }
135 ATF_TEST_CASE_CLEANUP(tc2)
136 {
137 ... second test case's cleanup ...
138 }
139
140 ATF_TEST_CASE(tc3);
141 ATF_TEST_CASE_BODY(tc3)
142 {
143 ... third test case's body ...
144 }
145
146 ... additional test cases ...
147
148 ATF_INIT_TEST_CASES(tcs)
149 {
150 ATF_ADD_TEST_CASE(tcs, tc1);
151 ATF_ADD_TEST_CASE(tcs, tc2);
152 ATF_ADD_TEST_CASE(tcs, tc3);
153 ... add additional test cases ...
154 }
155
156 Definition of test cases
157 Test cases have an identifier and are composed of three different parts:
158 the header, the body and an optional cleanup routine, all of which are
159 described in atf-test-case(4). To define test cases, one can use the
160 ATF_TEST_CASE(), ATF_TEST_CASE_WITH_CLEANUP() or the
161 ATF_TEST_CASE_WITHOUT_HEAD() macros, which take a single parameter speci‐
162 fiying the test case's name. ATF_TEST_CASE(), requires to define a head
163 and a body for the test case, ATF_TEST_CASE_WITH_CLEANUP() requires to
164 define a head, a body and a cleanup for the test case and
165 ATF_TEST_CASE_WITHOUT_HEAD() requires only a body for the test case. It
166 is important to note that these do not set the test case up for execution
167 when the program is run. In order to do so, a later registration is
168 needed through the ATF_ADD_TEST_CASE() macro detailed in Program
169 initialization.
170
171 Later on, one must define the three parts of the body by means of three
172 functions. Their headers are given by the ATF_TEST_CASE_HEAD(),
173 ATF_TEST_CASE_BODY() and ATF_TEST_CASE_CLEANUP() macros, all of which
174 take the test case's name. Following each of these, a block of code is
175 expected, surrounded by the opening and closing brackets.
176
177 Additionally, the ATF_TEST_CASE_NAME() macro can be used to obtain the
178 name of the class corresponding to a particular test case, as the name is
179 internally manged by the library to prevent clashes with other user iden‐
180 tifiers. Similarly, the ATF_TEST_CASE_USE() macro can be executed on a
181 particular test case to mark it as "used" and thus prevent compiler warn‐
182 ings regarding unused symbols. Note that you should never have to use
183 these macros during regular operation.
184
185 Program initialization
186 The library provides a way to easily define the test program's main()
187 function. You should never define one on your own, but rely on the li‐
188 brary to do it for you. This is done by using the ATF_INIT_TEST_CASES()
189 macro, which is passed the name of the list that will hold the test
190 cases. This name can be whatever you want as long as it is a valid vari‐
191 able value.
192
193 After the macro, you are supposed to provide the body of a function,
194 which should only use the ATF_ADD_TEST_CASE() macro to register the test
195 cases the test program will execute. The first parameter of this macro
196 matches the name you provided in the former call.
197
198 Header definitions
199 The test case's header can define the meta-data by using the set_md_var()
200 method, which takes two parameters: the first one specifies the meta-data
201 variable to be set and the second one specifies its value. Both of them
202 are strings.
203
204 Configuration variables
205 The test case has read-only access to the current configuration variables
206 by means of the bool has_config_var() and the std::string
207 get_config_var() methods, which can be called in any of the three parts
208 of a test case.
209
210 Access to the source directory
211 It is possible to get the path to the test case's source directory from
212 any of its three components by querying the ‘srcdir’ configuration vari‐
213 able.
214
215 Requiring programs
216 Aside from the require.progs meta-data variable available in the header
217 only, one can also check for additional programs in the test case's body
218 by using the require_prog() function, which takes the base name or full
219 path of a single binary. Relative paths are forbidden. If it is not
220 found, the test case will be automatically skipped.
221
222 Test case finalization
223 The test case finalizes either when the body reaches its end, at which
224 point the test is assumed to have passed, or at any explicit call to
225 ATF_PASS(), ATF_FAIL() or ATF_SKIP(). These three macros terminate the
226 execution of the test case immediately. The cleanup routine will be pro‐
227 cessed afterwards in a completely automated way, regardless of the test
228 case's termination reason.
229
230 ATF_PASS() does not take any parameters. ATF_FAIL() and ATF_SKIP() take
231 a single string that describes why the test case failed or was skipped,
232 respectively. It is very important to provide a clear error message in
233 both cases so that the user can quickly know why the test did not pass.
234
235 Expectations
236 Everything explained in the previous section changes when the test case
237 expectations are redefined by the programmer.
238
239 Each test case has an internal state called ‘expect’ that describes what
240 the test case expectations are at any point in time. The value of this
241 property can change during execution by any of:
242
243 expect_death(reason)
244 Expects the test case to exit prematurely regardless of the na‐
245 ture of the exit.
246
247 expect_exit(exitcode, reason)
248 Expects the test case to exit cleanly. If exitcode is not ‘-1’,
249 the runtime engine will validate that the exit code of the test
250 case matches the one provided in this call. Otherwise, the exact
251 value will be ignored.
252
253 expect_fail(reason)
254 Any failure (be it fatal or non-fatal) raised in this mode is
255 recorded. However, such failures do not report the test case as
256 failed; instead, the test case finalizes cleanly and is reported
257 as ‘expected failure’; this report includes the provided reason
258 as part of it. If no error is raised while running in this mode,
259 then the test case is reported as ‘failed’.
260
261 This mode is useful to reproduce actual known bugs in tests.
262 Whenever the developer fixes the bug later on, the test case will
263 start reporting a failure, signaling the developer that the test
264 case must be adjusted to the new conditions. In this situation,
265 it is useful, for example, to set reason as the bug number for
266 tracking purposes.
267
268 expect_pass()
269 This is the normal mode of execution. In this mode, any failure
270 is reported as such to the user and the test case is marked as
271 ‘failed’.
272
273 expect_race(reason)
274 Any failure or timeout during the execution of the test case will
275 be considered as if a race condition has been triggered and re‐
276 ported as such. If no problems arise, the test will continue ex‐
277 ecution as usual.
278
279 expect_signal(signo, reason)
280 Expects the test case to terminate due to the reception of a sig‐
281 nal. If signo is not ‘-1’, the runtime engine will validate that
282 the signal that terminated the test case matches the one provided
283 in this call. Otherwise, the exact value will be ignored.
284
285 expect_timeout(reason)
286 Expects the test case to execute for longer than its timeout.
287
288 Helper macros for common checks
289 The library provides several macros that are very handy in multiple situ‐
290 ations. These basically check some condition after executing a given
291 statement or processing a given expression and, if the condition is not
292 met, they automatically call ATF_FAIL() with an appropriate error mes‐
293 sage.
294
295 ATF_REQUIRE() takes an expression and raises a failure if it evaluates to
296 false.
297
298 ATF_REQUIRE_EQ() takes two expressions and raises a failure if the two do
299 not evaluate to the same exact value. The common style is to put the ex‐
300 pected value in the first parameter and the actual value in the second
301 parameter.
302
303 ATF_REQUIRE_IN() takes an element and a collection and validates that the
304 element is present in the collection.
305
306 ATF_REQUIRE_MATCH() takes a regular expression and a string and raises a
307 failure if the regular expression does not match the string.
308
309 ATF_REQUIRE_NOT_IN() takes an element and a collection and validates that
310 the element is not present in the collection.
311
312 ATF_REQUIRE_THROW() takes the name of an exception and a statement and
313 raises a failure if the statement does not throw the specified exception.
314 ATF_REQUIRE_THROW_RE() takes the name of an exception, a regular expre‐
315 sion and a statement and raises a failure if the statement does not throw
316 the specified exception and if the message of the exception does not
317 match the regular expression.
318
319 ATF_CHECK_ERRNO() and ATF_REQUIRE_ERRNO() take, first, the error code
320 that the check is expecting to find in the errno variable and, second, a
321 boolean expression that, if evaluates to true, means that a call failed
322 and errno has to be checked against the first value.
323
324 Utility functions
325 The following functions are provided as part of the atf-c++ API to sim‐
326 plify the creation of a variety of tests. In particular, these are use‐
327 ful to write tests for command-line interfaces.
328
329 void atf::utils::cat_file(const std::string& path,
330 const std::string& prefix)
331
332 Prints the contents of path to the standard output, prefixing every
333 line with the string in prefix.
334
335 bool atf::utils::compare_file(const std::string& path,
336 const std::string& contents)
337
338 Returns true if the given path matches exactly the expected inlined
339 contents.
340
341 void atf::utils::copy_file(const std::string& source,
342 const std::string& destination)
343
344 Copies the file source to destination. The permissions of the file
345 are preserved during the code.
346
347 void atf::utils::create_file(const std::string& path,
348 const std::string& contents)
349
350 Creates file with the text given in contents.
351
352 void atf::utils::file_exists(const std::string& path)
353
354 Checks if path exists.
355
356 pid_t atf::utils::fork(void)
357
358 Forks a process and redirects the standard output and standard er‐
359 ror of the child to files for later validation with
360 atf::utils::wait(). Fails the test case if the fork fails, so this
361 does not return an error.
362
363 bool atf::utils::grep_collection(const std::string& regexp,
364 const Collection& collection)
365
366 Searches for the regular expression regexp in any of the strings
367 contained in the collection. This is a template that accepts any
368 one-dimensional container of strings.
369
370 bool atf::utils::grep_file(const std::string& regexp,
371 const std::string& path)
372
373 Searches for the regular expression regexp in the file path. The
374 variable arguments are used to construct the regular expression.
375
376 bool atf::utils::grep_string(const std::string& regexp,
377 const std::string& str)
378
379 Searches for the regular expression regexp in the string str.
380 void atf::utils::redirect(const int fd, const std::string& path)
381
382 Redirects the given file descriptor fd to the file path. This
383 function exits the process in case of an error and does not prop‐
384 erly mark the test case as failed. As a result, it should only be
385 used in subprocesses of the test case; specially those spawned by
386 atf::utils::fork().
387
388 void atf::utils::wait(const pid_t pid, const int expected_exit_status,
389 const std::string& expected_stdout, const std::string& expected_stderr)
390
391 Waits and validates the result of a subprocess spawned with
392 atf::utils::wait(). The validation involves checking that the sub‐
393 process exited cleanly and returned the code specified in
394 expected_exit_status and that its standard output and standard er‐
395 ror match the strings given in expected_stdout and expected_stderr.
396
397 If any of the expected_stdout or expected_stderr strings are pre‐
398 fixed with ‘save:’, then they specify the name of the file into
399 which to store the stdout or stderr of the subprocess, and no com‐
400 parison is performed.
401
403 The following variables are recognized by atf-c++ but should not be over‐
404 ridden other than for testing purposes:
405
406 ATF_BUILD_CC Path to the C compiler.
407 ATF_BUILD_CFLAGS C compiler flags.
408 ATF_BUILD_CPP Path to the C/C++ preprocessor.
409 ATF_BUILD_CPPFLAGS C/C++ preprocessor flags.
410 ATF_BUILD_CXX Path to the C++ compiler.
411 ATF_BUILD_CXXFLAGS C++ compiler flags.
412
414 The following shows a complete test program with a single test case that
415 validates the addition operator:
416
417 #include <atf-c++.hpp>
418
419 ATF_TEST_CASE(addition);
420 ATF_TEST_CASE_HEAD(addition)
421 {
422 set_md_var("descr", "Sample tests for the addition operator");
423 }
424 ATF_TEST_CASE_BODY(addition)
425 {
426 ATF_REQUIRE_EQ(0, 0 + 0);
427 ATF_REQUIRE_EQ(1, 0 + 1);
428 ATF_REQUIRE_EQ(1, 1 + 0);
429
430 ATF_REQUIRE_EQ(2, 1 + 1);
431
432 ATF_REQUIRE_EQ(300, 100 + 200);
433 }
434
435 ATF_TEST_CASE(open_failure);
436 ATF_TEST_CASE_HEAD(open_failure)
437 {
438 set_md_var("descr", "Sample tests for the open function");
439 }
440 ATF_TEST_CASE_BODY(open_failure)
441 {
442 ATF_REQUIRE_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
443 }
444
445 ATF_TEST_CASE(known_bug);
446 ATF_TEST_CASE_HEAD(known_bug)
447 {
448 set_md_var("descr", "Reproduces a known bug");
449 }
450 ATF_TEST_CASE_BODY(known_bug)
451 {
452 expect_fail("See bug number foo/bar");
453 ATF_REQUIRE_EQ(3, 1 + 1);
454 expect_pass();
455 ATF_REQUIRE_EQ(3, 1 + 2);
456 }
457
458 ATF_INIT_TEST_CASES(tcs)
459 {
460 ATF_ADD_TEST_CASE(tcs, addition);
461 ATF_ADD_TEST_CASE(tcs, open_failure);
462 ATF_ADD_TEST_CASE(tcs, known_bug);
463 }
464
466 atf-test-program(1), atf-test-case(4)
467
468BSD October 13, 2014 BSD