xref: /netbsd/external/bsd/atf/dist/atf-c/atf-c-api.3 (revision 6550d01e)
1.\"
2.\" Automated Testing Framework (atf)
3.\"
4.\" Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
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.
15.\"
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.
28.\"
29.Dd November 1, 2010
30.Dt ATF-C-API 3
31.Os
32.Sh NAME
33.Nm ATF_CHECK ,
34.Nm ATF_CHECK_MSG ,
35.Nm ATF_CHECK_EQ ,
36.Nm ATF_CHECK_EQ_MSG ,
37.Nm ATF_CHECK_STREQ ,
38.Nm ATF_CHECK_STREQ_MSG ,
39.Nm ATF_CHECK_ERRNO ,
40.Nm ATF_REQUIRE ,
41.Nm ATF_REQUIRE_MSG ,
42.Nm ATF_REQUIRE_EQ ,
43.Nm ATF_REQUIRE_EQ_MSG ,
44.Nm ATF_REQUIRE_STREQ ,
45.Nm ATF_REQUIRE_STREQ_MSG ,
46.Nm ATF_REQUIRE_ERRNO ,
47.Nm ATF_TC ,
48.Nm ATF_TC_BODY ,
49.Nm ATF_TC_BODY_NAME ,
50.Nm ATF_TC_CLEANUP ,
51.Nm ATF_TC_CLEANUP_NAME ,
52.Nm ATF_TC_HEAD ,
53.Nm ATF_TC_HEAD_NAME ,
54.Nm ATF_TC_NAME ,
55.Nm ATF_TC_WITH_CLEANUP ,
56.Nm ATF_TC_WITHOUT_HEAD ,
57.Nm ATF_TP_ADD_TC ,
58.Nm ATF_TP_ADD_TCS ,
59.Nm atf_no_error ,
60.Nm atf_tc_expect_death ,
61.Nm atf_tc_expect_exit ,
62.Nm atf_tc_expect_fail ,
63.Nm atf_tc_expect_pass ,
64.Nm atf_tc_expect_signal ,
65.Nm atf_tc_expect_timeout ,
66.Nm atf_tc_fail ,
67.Nm atf_tc_fail_nonfatal ,
68.Nm atf_tc_pass ,
69.Nm atf_tc_skip
70.Nd C API to write ATF-based test programs
71.Sh SYNOPSIS
72.In atf-c.h
73.Fn ATF_CHECK "expression"
74.Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
75.Fn ATF_CHECK_EQ "expression_1" "expression_2"
76.Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
77.Fn ATF_CHECK_STREQ "string_1" "string_2"
78.Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
79.Fn ATF_CHECK_ERRNO "exp_errno" "bool_expression"
80.Fn ATF_REQUIRE "expression"
81.Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
82.Fn ATF_REQUIRE_EQ "expression_1" "expression_2"
83.Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
84.Fn ATF_REQUIRE_STREQ "string_1" "string_2"
85.Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
86.Fn ATF_REQUIRE_ERRNO "exp_errno" "bool_expression"
87.Fn ATF_TC "name"
88.Fn ATF_TC_BODY "name" "tc"
89.Fn ATF_TC_BODY_NAME "name"
90.Fn ATF_TC_CLEANUP "name" "tc"
91.Fn ATF_TC_CLEANUP_NAME "name"
92.Fn ATF_TC_HEAD "name" "tc"
93.Fn ATF_TC_HEAD_NAME "name"
94.Fn ATF_TC_NAME "name"
95.Fn ATF_TC_WITH_CLEANUP "name"
96.Fn ATF_TC_WITHOUT_HEAD "name"
97.Fn ATF_TP_ADD_TC "tp_name" "tc_name"
98.Fn ATF_TP_ADD_TCS "tp_name"
99.Fn atf_no_error
100.Fn atf_tc_expect_death "reason" "..."
101.Fn atf_tc_expect_exit "exitcode" "reason" "..."
102.Fn atf_tc_expect_fail "reason" "..."
103.Fn atf_tc_expect_pass
104.Fn atf_tc_expect_signal "signo" "reason" "..."
105.Fn atf_tc_expect_timeout "reason" "..."
106.Fn atf_tc_fail "reason"
107.Fn atf_tc_fail_nonfatal "reason"
108.Fn atf_tc_pass
109.Fn atf_tc_skip "reason"
110.Sh DESCRIPTION
111The ATF
112.Pp
113C-based test programs always follow this template:
114.Bd -literal -offset indent
115.Ns ... C-specific includes go here ...
116
117#include <atf-c.h>
118
119ATF_TC(tc1);
120ATF_TC_HEAD(tc1, tc)
121{
122    ... first test case's header ...
123}
124ATF_TC_BODY(tc1, tc)
125{
126    ... first test case's body ...
127}
128
129ATF_TC_WITH_CLEANUP(tc2);
130ATF_TC_HEAD(tc2, tc)
131{
132    ... second test case's header ...
133}
134ATF_TC_BODY(tc2, tc)
135{
136    ... second test case's body ...
137}
138ATF_TC_CLEANUP(tc2, tc)
139{
140    ... second test case's cleanup ...
141}
142
143ATF_TC_WITHOUT_HEAD(tc3);
144ATF_TC_BODY(tc3, tc)
145{
146    ... third test case's body ...
147}
148
149.Ns ... additional test cases ...
150
151ATF_TP_ADD_TCS(tp)
152{
153    ATF_TP_ADD_TC(tcs, tc1);
154    ATF_TP_ADD_TC(tcs, tc2);
155    ATF_TP_ADD_TC(tcs, tc3);
156    ... add additional test cases ...
157
158    return atf_no_error();
159}
160.Ed
161.Ss Definition of test cases
162Test cases have an identifier and are composed of three different parts:
163the header, the body and an optional cleanup routine, all of which are
164described in
165.Xr atf-test-case 4 .
166To define test cases, one can use the
167.Fn ATF_TC ,
168.Fn ATF_TC_WITH_CLEANUP
169or the
170.Fn ATF_TC_WITHOUT_HEAD
171macros, which take a single parameter specifiying the test case's name.
172.Fn ATF_TC ,
173requires to define a head and a body for the test case,
174.Fn ATF_TC_WITH_CLEANUP
175requires to define a head, a body and a cleanup for the test case and
176.Fn ATF_TC_WITHOUT_HEAD
177requires only a body for the test case.
178It is important to note that these
179.Em do not
180set the test case up for execution when the program is run.
181In order to do so, a later registration is needed with the
182.Fn ATF_TP_ADD_TC
183macro detailed in
184.Sx Program initialization .
185.Pp
186Later on, one must define the three parts of the body by means of three
187functions.
188Their headers are given by the
189.Fn ATF_TC_HEAD ,
190.Fn ATF_TC_BODY
191and
192.Fn ATF_TC_CLEANUP
193macros, all of which take the test case name provided to the
194.Fn ATF_TC
195.Fn ATF_TC_WITH_CLEANUP ,
196or
197.Fn ATF_TC_WITHOUT_HEAD
198macros and the name of the variable that will hold a pointer to the
199test case data.
200Following each of these, a block of code is expected, surrounded by the
201opening and closing brackets.
202.Ss Program initialization
203The library provides a way to easily define the test program's
204.Fn main
205function.
206You should never define one on your own, but rely on the
207library to do it for you.
208This is done by using the
209.Fn ATF_TP_ADD_TCS
210macro, which is passed the name of the object that will hold the test
211cases; i.e. the test program instance.
212This name can be whatever you want as long as it is a valid variable
213identifier.
214.Pp
215After the macro, you are supposed to provide the body of a function, which
216should only use the
217.Fn ATF_TP_ADD_TC
218macro to register the test cases the test program will execute and return
219a success error code.
220The first parameter of this macro matches the name you provided in the
221former call.
222The success status can be returned using the
223.Fn atf_no_error
224function.
225.Ss Header definitions
226The test case's header can define the meta-data by using the
227.Fn atf_tc_set_md_var
228method, which takes three parameters: the first one points to the test
229case data, the second one specifies the meta-data variable to be set
230and the third one specifies its value.
231Both of them are strings.
232.Ss Configuration variables
233The test case has read-only access to the current configuration variables
234by means of the
235.Ft bool
236.Fn atf_tc_has_config_var ,
237.Ft const char *
238.Fn atf_tc_get_config_var ,
239.Ft const char *
240.Fn atf_tc_get_config_var_wd ,
241.Ft bool
242.Fn atf_tc_get_config_var_as_bool ,
243.Ft bool
244.Fn atf_tc_get_config_var_as_bool_wd ,
245.Ft long
246.Fn atf_tc_get_config_var_as_long ,
247and the
248.Ft long
249.Fn atf_tc_get_config_var_as_long_wd
250functions, which can be called in any of the three parts of a test case.
251.Ss Access to the source directory
252It is possible to get the path to the test case's source directory from any
253of its three components by querying the
254.Sq srcdir
255configuration variable.
256.Ss Requiring programs
257Aside from the
258.Va require.progs
259meta-data variable available in the header only, one can also check for
260additional programs in the test case's body by using the
261.Fn atf_tc_require_prog
262function, which takes the base name or full path of a single binary.
263Relative paths are forbidden.
264If it is not found, the test case will be automatically skipped.
265.Ss Test case finalization
266The test case finalizes either when the body reaches its end, at which
267point the test is assumed to have
268.Em passed ,
269unless any non-fatal errors were raised using
270.Fn atf_tc_fail_nonfatal ,
271or at any explicit call to
272.Fn atf_tc_pass ,
273.Fn atf_tc_fail
274or
275.Fn atf_tc_skip .
276These three functions terminate the execution of the test case immediately.
277The cleanup routine will be processed afterwards in a completely automated
278way, regardless of the test case's termination reason.
279.Pp
280.Fn atf_tc_pass
281does not take any parameters.
282.Fn atf_tc_fail ,
283.Fn atf_tc_fail_nonfatal
284and
285.Fn atf_tc_skip
286take a format string and a variable list of parameters, which describe, in
287a user-friendly manner, why the test case failed or was skipped,
288respectively.
289It is very important to provide a clear error message in both cases so that
290the user can quickly know why the test did not pass.
291.Ss Expectations
292Everything explained in the previous section changes when the test case
293expectations are redefined by the programmer.
294.Pp
295Each test case has an internal state called
296.Sq expect
297that describes what the test case expectations are at any point in time.
298The value of this property can change during execution by any of:
299.Bl -tag -width indent
300.It Fn atf_tc_expect_death "reason" "..."
301Expects the test case to exit prematurely regardless of the nature of the
302exit.
303.It Fn atf_tc_expect_exit "exitcode" "reason" "..."
304Expects the test case to exit cleanly.
305If
306.Va exitcode
307is not
308.Sq -1 ,
309.Xr atf-run 1
310will validate that the exit code of the test case matches the one provided
311in this call.
312Otherwise, the exact value will be ignored.
313.It Fn atf_tc_expect_fail "reason" "..."
314Any failure (be it fatal or non-fatal) raised in this mode is recorded.
315However, such failures do not report the test case as failed; instead, the
316test case finalizes cleanly and is reported as
317.Sq expected failure ;
318this report includes the provided
319.Fa reason
320as part of it.
321If no error is raised while running in this mode, then the test case is
322reported as
323.Sq failed .
324.Pp
325This mode is useful to reproduce actual known bugs in tests.
326Whenever the developer fixes the bug later on, the test case will start
327reporting a failure, signaling the developer that the test case must be
328adjusted to the new conditions.
329In this situation, it is useful, for example, to set
330.Fa reason
331as the bug number for tracking purposes.
332.It Fn atf_tc_expect_pass
333This is the normal mode of execution.
334In this mode, any failure is reported as such to the user and the test case
335is marked as
336.Sq failed .
337.It Fn atf_tc_expect_signal "signo" "reason" "..."
338Expects the test case to terminate due to the reception of a signal.
339If
340.Va signo
341is not
342.Sq -1 ,
343.Xr atf-run 1
344will validate that the signal that terminated the test case matches the one
345provided in this call.
346Otherwise, the exact value will be ignored.
347.It Fn atf_tc_expect_timeout "reason" "..."
348Expects the test case to execute for longer than its timeout.
349.El
350.Ss Helper macros for common checks
351The library provides several macros that are very handy in multiple
352situations.
353These basically check some condition after executing a given statement or
354processing a given expression and, if the condition is not met, they
355report the test case as failed.
356.Pp
357The
358.Sq REQUIRE
359variant of the macros immediately abort the test case as soon as an error
360condition is detected by calling the
361.Fn atf_tc_fail
362function.
363Use this variant whenever it makes now sense to continue the execution of a
364test case when the checked condition is not met.
365The
366.Sq CHECK
367variant, on the other hand, reports a failure as soon as it is encountered
368using the
369.Fn atf_tc_fail_nonfatal
370function, but the execution of the test case continues as if nothing had
371happened.
372Use this variant whenever the checked condition is important as a result of
373the test case, but there are other conditions that can be subsequently
374checked on the same run without aborting.
375.Pp
376Additionally, the
377.Sq MSG
378variants take an extra set of parameters to explicitly specify the failure
379message.
380This failure message is formatted according to the
381.Xr printf 3
382formatters.
383.Pp
384.Fn ATF_CHECK ,
385.Fn ATF_CHECK_MSG ,
386.Fn ATF_REQUIRE
387and
388.Fn ATF_REQUIRE_MSG
389take an expression and fail if the expression evaluates to false.
390.Pp
391.Fn ATF_CHECK_EQ ,
392.Fn ATF_CHECK_EQ_MSG ,
393.Fn ATF_REQUIRE_EQ
394and
395.Fn ATF_REQUIRE_EQ_MSG
396take two expressions and fail if the two evaluated values are not equal.
397.Pp
398.Fn ATF_CHECK_STREQ ,
399.Fn ATF_CHECK_STREQ_MSG ,
400.Fn ATF_REQUIRE_STREQ
401and
402.Fn ATF_REQUIRE_STREQ_MSG
403take two strings and fail if the two are not equal character by character.
404.Pp
405.Fn ATF_CHECK_ERRNO
406and
407.Fn ATF_REQUIRE_ERRNO
408take, first, the error code that the check is expecting to find in the
409.Va errno
410variable and, second, a boolean expression that, if evaluates to true,
411means that a call failed and
412.Va errno
413has to be checked against the first value.
414.Sh EXAMPLES
415The following shows a complete test program with a single test case that
416validates the addition operator:
417.Bd -literal -offset indent
418#include <atf-c.h>
419
420ATF_TC(addition);
421ATF_TC_HEAD(addition, tc)
422{
423    atf_tc_set_md_var(tc, "descr",
424                      "Sample tests for the addition operator");
425}
426ATF_TC_BODY(addition, tc)
427{
428    ATF_CHECK_EQ(0 + 0, 0);
429    ATF_CHECK_EQ(0 + 1, 1);
430    ATF_CHECK_EQ(1 + 0, 1);
431
432    ATF_CHECK_EQ(1 + 1, 2);
433
434    ATF_CHECK_EQ(100 + 200, 300);
435}
436
437ATF_TC(string_formatting);
438ATF_TC_HEAD(string_formatting, tc)
439{
440    atf_tc_set_md_var(tc, "descr",
441                      "Sample tests for the snprintf");
442}
443ATF_TC_BODY(string_formatting, tc)
444{
445    char buf[1024];
446    snprintf(buf, sizeof(buf), "a %s", "string");
447    ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
448}
449
450ATF_TC(open_failure);
451ATF_TC_HEAD(open_failure, tc)
452{
453    atf_tc_set_md_var(tc, "descr",
454                      "Sample tests for the open function");
455}
456ATF_TC_BODY(open_failure, tc)
457{
458    ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
459}
460
461ATF_TC(known_bug);
462ATF_TC_HEAD(known_bug, tc)
463{
464    atf_tc_set_md_var(tc, "descr",
465                      "Reproduces a known bug");
466}
467ATF_TC_BODY(known_bug, tc)
468{
469    atf_tc_expect_fail("See bug number foo/bar");
470    ATF_CHECK_EQ(3, 1 + 1);
471    atf_tc_expect_pass();
472    ATF_CHECK_EQ(3, 1 + 2);
473}
474
475ATF_TP_ADD_TCS(tp)
476{
477    ATF_TP_ADD_TC(tp, addition);
478    ATF_TP_ADD_TC(tp, string_formatting);
479    ATF_TP_ADD_TC(tp, open_failure);
480    ATF_TP_ADD_TC(tp, known_bug);
481
482    return atf_no_error();
483}
484.Ed
485.Sh SEE ALSO
486.Xr atf-test-program 1 ,
487.Xr atf-test-case 4 ,
488.Xr atf 7
489