xref: /netbsd-src/external/bsd/atf/dist/atf-c/atf-c-api.3 (revision c505c4429840c353a86d4eb53b5e2bfc0092264e)
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 June 28, 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
237and the
238.Ft const char *
239.Fn atf_tc_get_config_var
240methods, which can be called in any of the three parts of a test case.
241.Ss Access to the source directory
242It is possible to get the path to the test case's source directory from any
243of its three components by querying the
244.Sq srcdir
245configuration variable.
246.Ss Requiring programs
247Aside from the
248.Va require.progs
249meta-data variable available in the header only, one can also check for
250additional programs in the test case's body by using the
251.Fn atf_tc_require_prog
252function, which takes the base name or full path of a single binary.
253Relative paths are forbidden.
254If it is not found, the test case will be automatically skipped.
255.Ss Test case finalization
256The test case finalizes either when the body reaches its end, at which
257point the test is assumed to have
258.Em passed ,
259unless any non-fatal errors were raised using
260.Fn atf_tc_fail_nonfatal ,
261or at any explicit call to
262.Fn atf_tc_pass ,
263.Fn atf_tc_fail
264or
265.Fn atf_tc_skip .
266These three functions terminate the execution of the test case immediately.
267The cleanup routine will be processed afterwards in a completely automated
268way, regardless of the test case's termination reason.
269.Pp
270.Fn atf_tc_pass
271does not take any parameters.
272.Fn atf_tc_fail ,
273.Fn atf_tc_fail_nonfatal
274and
275.Fn atf_tc_skip
276take a format string and a variable list of parameters, which describe, in
277a user-friendly manner, why the test case failed or was skipped,
278respectively.
279It is very important to provide a clear error message in both cases so that
280the user can quickly know why the test did not pass.
281.Ss Expectations
282Everything explained in the previous section changes when the test case
283expectations are redefined by the programmer.
284.Pp
285Each test case has an internal state called
286.Sq expect
287that describes what the test case expectations are at any point in time.
288The value of this property can change during execution by any of:
289.Bl -tag -width indent
290.It Fn atf_tc_expect_death "reason" "..."
291Expects the test case to exit prematurely regardless of the nature of the
292exit.
293.It Fn atf_tc_expect_exit "exitcode" "reason" "..."
294Expects the test case to exit cleanly.
295If
296.Va exitcode
297is not
298.Sq -1 ,
299.Xr atf-run 1
300will validate that the exit code of the test case matches the one provided
301in this call.
302Otherwise, the exact value will be ignored.
303.It Fn atf_tc_expect_fail "reason" "..."
304Any failure (be it fatal or non-fatal) raised in this mode is recorded.
305However, such failures do not report the test case as failed; instead, the
306test case finalizes cleanly and is reported as
307.Sq expected failure ;
308this report includes the provided
309.Fa reason
310as part of it.
311If no error is raised while running in this mode, then the test case is
312reported as
313.Sq failed .
314.Pp
315This mode is useful to reproduce actual known bugs in tests.
316Whenever the developer fixes the bug later on, the test case will start
317reporting a failure, signaling the developer that the test case must be
318adjusted to the new conditions.
319In this situation, it is useful, for example, to set
320.Fa reason
321as the bug number for tracking purposes.
322.It Fn atf_tc_expect_pass
323This is the normal mode of execution.
324In this mode, any failure is reported as such to the user and the test case
325is marked as
326.Sq failed .
327.It Fn atf_tc_expect_signal "signo" "reason" "..."
328Expects the test case to terminate due to the reception of a signal.
329If
330.Va signo
331is not
332.Sq -1 ,
333.Xr atf-run 1
334will validate that the signal that terminated the test case matches the one
335provided in this call.
336Otherwise, the exact value will be ignored.
337.It Fn atf_tc_expect_timeout "reason" "..."
338Expects the test case to execute for longer than its timeout.
339.El
340.Ss Helper macros for common checks
341The library provides several macros that are very handy in multiple
342situations.
343These basically check some condition after executing a given statement or
344processing a given expression and, if the condition is not met, they
345report the test case as failed.
346.Pp
347The
348.Sq REQUIRE
349variant of the macros immediately abort the test case as soon as an error
350condition is detected by calling the
351.Fn atf_tc_fail
352function.
353Use this variant whenever it makes now sense to continue the execution of a
354test case when the checked condition is not met.
355The
356.Sq CHECK
357variant, on the other hand, reports a failure as soon as it is encountered
358using the
359.Fn atf_tc_fail_nonfatal
360function, but the execution of the test case continues as if nothing had
361happened.
362Use this variant whenever the checked condition is important as a result of
363the test case, but there are other conditions that can be subsequently
364checked on the same run without aborting.
365.Pp
366Additionally, the
367.Sq MSG
368variants take an extra set of parameters to explicitly specify the failure
369message.
370This failure message is formatted according to the
371.Xr printf 3
372formatters.
373.Pp
374.Fn ATF_CHECK ,
375.Fn ATF_CHECK_MSG ,
376.Fn ATF_REQUIRE
377and
378.Fn ATF_REQUIRE_MSG
379take an expression and fail if the expression evaluates to false.
380.Pp
381.Fn ATF_CHECK_EQ ,
382.Fn ATF_CHECK_EQ_MSG ,
383.Fn ATF_REQUIRE_EQ
384and
385.Fn ATF_REQUIRE_EQ_MSG
386take two expressions and fail if the two evaluated values are not equal.
387.Pp
388.Fn ATF_CHECK_STREQ ,
389.Fn ATF_CHECK_STREQ_MSG ,
390.Fn ATF_REQUIRE_STREQ
391and
392.Fn ATF_REQUIRE_STREQ_MSG
393take two strings and fail if the two are not equal character by character.
394.Pp
395.Fn ATF_CHECK_ERRNO
396and
397.Fn ATF_REQUIRE_ERRNO
398take, first, the error code that the check is expecting to find in the
399.Va errno
400variable and, second, a boolean expression that, if evaluates to true,
401means that a call failed and
402.Va errno
403has to be checked against the first value.
404.Sh EXAMPLES
405The following shows a complete test program with a single test case that
406validates the addition operator:
407.Bd -literal -offset indent
408#include <atf-c.h>
409
410ATF_TC(addition);
411ATF_TC_HEAD(addition, tc)
412{
413    atf_tc_set_md_var(tc, "descr",
414                      "Sample tests for the addition operator");
415}
416ATF_TC_BODY(addition, tc)
417{
418    ATF_CHECK_EQ(0 + 0, 0);
419    ATF_CHECK_EQ(0 + 1, 1);
420    ATF_CHECK_EQ(1 + 0, 1);
421
422    ATF_CHECK_EQ(1 + 1, 2);
423
424    ATF_CHECK_EQ(100 + 200, 300);
425}
426
427ATF_TC(string_formatting);
428ATF_TC_HEAD(string_formatting, tc)
429{
430    atf_tc_set_md_var(tc, "descr",
431                      "Sample tests for the snprintf");
432}
433ATF_TC_BODY(string_formatting, tc)
434{
435    char buf[1024];
436    snprintf(buf, sizeof(buf), "a %s", "string");
437    ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
438}
439
440ATF_TC(open_failure);
441ATF_TC_HEAD(open_failure, tc)
442{
443    atf_tc_set_md_var(tc, "descr",
444                      "Sample tests for the open function");
445}
446ATF_TC_BODY(open_failure, tc)
447{
448    ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1);
449}
450
451ATF_TC(known_bug);
452ATF_TC_HEAD(known_bug, tc)
453{
454    atf_tc_set_md_var(tc, "descr",
455                      "Reproduces a known bug");
456}
457ATF_TC_BODY(known_bug, tc)
458{
459    atf_tc_expect_fail("See bug number foo/bar");
460    ATF_CHECK_EQ(3, 1 + 1);
461    atf_tc_expect_pass();
462    ATF_CHECK_EQ(3, 1 + 2);
463}
464
465ATF_TP_ADD_TCS(tp)
466{
467    ATF_TP_ADD_TC(tp, addition);
468    ATF_TP_ADD_TC(tp, string_formatting);
469    ATF_TP_ADD_TC(tp, open_failure);
470    ATF_TP_ADD_TC(tp, known_bug);
471
472    return atf_no_error();
473}
474.Ed
475.Sh SEE ALSO
476.Xr atf-test-program 1 ,
477.Xr atf-test-case 4 ,
478.Xr atf 7
479