2 .\" Automated Testing Framework (atf)
4 .\" Copyright (c) 2008, 2009 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 ,
42 .Nm ATF_REQUIRE_EQ_MSG ,
43 .Nm ATF_REQUIRE_STREQ ,
44 .Nm ATF_REQUIRE_STREQ_MSG ,
47 .Nm ATF_TC_BODY_NAME ,
49 .Nm ATF_TC_CLEANUP_NAME ,
51 .Nm ATF_TC_HEAD_NAME ,
53 .Nm ATF_TC_WITH_CLEANUP ,
58 .Nm atf_tc_fail_nonfatal ,
61 .Nd C API to write ATF-based test programs
64 .Fn ATF_CHECK "expression"
65 .Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ...
66 .Fn ATF_CHECK_EQ "expression_1" "expression_2"
67 .Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
68 .Fn ATF_CHECK_STREQ "string_1" "string_2"
69 .Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
70 .Fn ATF_REQUIRE "expression"
71 .Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ...
72 .Fn ATF_REQUIRE_EQ "expression_1" "expression_2"
73 .Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ...
74 .Fn ATF_REQUIRE_STREQ "string_1" "string_2"
75 .Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ...
77 .Fn ATF_TC_BODY "name" "tc"
78 .Fn ATF_TC_BODY_NAME "name"
79 .Fn ATF_TC_CLEANUP "name" "tc"
80 .Fn ATF_TC_CLEANUP_NAME "name"
81 .Fn ATF_TC_HEAD "name" "tc"
82 .Fn ATF_TC_HEAD_NAME "name"
83 .Fn ATF_TC_NAME "name"
84 .Fn ATF_TC_WITH_CLEANUP "name"
85 .Fn ATF_TP_ADD_TC "tp_name"
86 .Fn ATF_TP_ADD_TCS "tp_name" "tc_name"
88 .Fn atf_tc_fail "reason"
90 .Fn atf_tc_fail_nonfatal "reason"
92 .Fn atf_tc_skip "reason"
96 C-based test programs always follow this template:
97 .Bd -literal -offset indent
98 .Ns ... C-specific includes go here ...
105 ... first test case's header ...
109 ... first test case's body ...
112 ATF_TC_WITH_CLEANUP(tc2);
115 ... second test case's header ...
119 ... second test case's body ...
121 ATF_TC_CLEANUP(tc2, tc)
123 ... second test case's cleanup ...
126 .Ns ... additional test cases ...
128 ATF_TP_ADD_TCS(tp, tcs)
130 ATF_TP_ADD_TC(tcs, tc1)
131 ATF_TP_ADD_TC(tcs, tc2)
132 ... add additional test cases ...
134 return atf_no_error();
137 .Ss Definition of test cases
138 Test cases have an identifier and are composed of three different parts:
139 the header, the body and an optional cleanup routine, all of which are
141 .Xr atf-test-case 8 .
142 To define test cases, one can use the
145 .Fn ATF_TC_WITH_CLEANUP
146 macros, which take a single parameter specifiying the test case's name.
147 The former does not allow the specification of a cleanup routine for the
148 test case while the latter does.
149 It is important to note that these
151 set the test case up for execution when the program is run.
152 In order to do so, a later registration is needed with the
155 .Sx Program initialization .
157 Later on, one must define the three parts of the body by means of three
159 Their headers are given by the
164 macros, all of which take the test case name provided to the
167 .Fn ATF_TC_WITH_CLEANUP
168 macros and the name of the variable that will hold a pointer to the
170 Following each of these, a block of code is expected, surrounded by the
171 opening and closing brackets.
172 .Ss Program initialization
173 The library provides a way to easily define the test program's
176 You should never define one on your own, but rely on the
177 library to do it for you.
178 This is done by using the
180 macro, which is passed the name of the object that will hold the test
181 cases; i.e. the test program instance.
182 This name can be whatever you want as long as it is a valid variable
185 After the macro, you are supposed to provide the body of a function, which
188 macro to register the test cases the test program will execute and return
189 a success error code.
190 The first parameter of this macro matches the name you provided in the
192 The success status can be returned using the
195 .Ss Header definitions
196 The test case's header can define the meta-data by using the
197 .Fn atf_tc_set_md_var
198 method, which takes three parameters: the first one points to the test
199 case data, the second one specifies the meta-data variable to be set
200 and the third one specifies its value.
201 Both of them are strings.
202 .Ss Configuration variables
203 The test case has read-only access to the current configuration variables
206 .Fn atf_tc_has_config_var
209 .Fn atf_tc_get_config_var
210 methods, which can be called in any of the three parts of a test case.
211 .Ss Access to the source directory
212 It is possible to get the path to the test case's source directory from any
213 of its three components by querying the
215 configuration variable.
216 .Ss Requiring programs
219 meta-data variable available in the header only, one can also check for
220 additional programs in the test case's body by using the
221 .Fn atf_tc_require_prog
222 function, which takes the base name or full path of a single binary.
223 Relative paths are forbidden.
224 If it is not found, the test case will be automatically skipped.
225 .Ss Test case finalization
226 The test case finalizes either when the body reaches its end, at which
227 point the test is assumed to have
229 unless any non-fatal errors were raised using
230 .Fn atf_tc_fail_nonfatal ,
231 or at any explicit call to
236 These three functions terminate the execution of the test case immediately.
237 The cleanup routine will be processed afterwards in a completely automated
238 way, regardless of the test case's termination reason.
241 does not take any parameters.
243 .Fn atf_tc_fail_nonfatal
246 take a format string and a variable list of parameters, which describe, in
247 a user-friendly manner, why the test case failed or was skipped,
249 It is very important to provide a clear error message in both cases so that
250 the user can quickly know why the test did not pass.
251 .Ss Helper macros for common checks
252 The library provides several macros that are very handy in multiple
254 These basically check some condition after executing a given statement or
255 processing a given expression and, if the condition is not met, they
256 report the test case as failed.
260 variant of the macros immediately abort the test case as soon as an error
261 condition is detected by calling the
264 Use this variant whenever it makes now sense to continue the execution of a
265 test case when the checked condition is not met.
268 variant, on the other hand, reports a failure as soon as it is encountered
270 .Fn atf_tc_fail_nonfatal
271 function, but the execution of the test case continues as if nothing had
273 Use this variant whenever the checked condition is important as a result of
274 the test case, but there are other conditions that can be subsequently
275 checked on the same run without aborting.
279 variants take an extra set of parameters to explicitly specify the failure
281 This failure message is formatted according to the
290 take an expression and fail if the expression evaluates to false.
293 .Fn ATF_CHECK_EQ_MSG ,
296 .Fn ATF_REQUIRE_EQ_MSG
297 take two expressions and fail if the two evaluated values are not equal.
299 .Fn ATF_CHECK_STREQ ,
300 .Fn ATF_CHECK_STREQ_MSG ,
301 .Fn ATF_REQUIRE_STREQ
303 .Fn ATF_REQUIRE_STREQ_MSG
304 take two strings and fail if the two are not equal character by character.
306 The following shows a complete test program with a single test case that
307 validates the addition operator:
308 .Bd -literal -offset indent
312 ATF_TC_HEAD(addition, tc)
314 atf_tc_set_md_var(tc, "descr",
315 "Sample tests for the addition operator");
317 ATF_TC_BODY(addition, tc)
319 ATF_CHECK_EQ(0 + 0, 0);
320 ATF_CHECK_EQ(0 + 1, 1);
321 ATF_CHECK_EQ(1 + 0, 1);
323 ATF_CHECK_EQ(1 + 1, 2);
325 ATF_CHECK_EQ(100 + 200, 300);
328 ATF_TC(string_formatting);
329 ATF_TC_HEAD(string_formatting, tc)
331 atf_tc_set_md_var(tc, "descr",
332 "Sample tests for the snprintf");
334 ATF_TC_BODY(string_formatting, tc)
337 snprintf(buf, sizeof(buf), "a %s", "string");
338 ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working");
343 ATF_TP_ADD_TC(tp, addition);
344 ATF_TP_ADD_TC(tp, string_formatting);
346 return atf_no_error();
350 .Xr atf-test-program 1 ,