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.
33 .Nm atf_add_test_case ,
38 .Nm atf_expect_death ,
42 .Nm atf_expect_signal ,
43 .Nm atf_expect_timeout ,
48 .Nm atf_require_prog ,
52 .Nd POSIX shell API to write ATF-based test programs
54 .Fn atf_add_test_case "name"
55 .Fn atf_check "command"
56 .Fn atf_check_equal "expr1" "expr2"
57 .Fn atf_config_get "var_name"
58 .Fn atf_config_has "var_name"
59 .Fn atf_expect_death "reason" "..."
60 .Fn atf_expect_exit "exitcode" "reason" "..."
61 .Fn atf_expect_fail "reason" "..."
63 .Fn atf_expect_signal "signo" "reason" "..."
64 .Fn atf_expect_timeout "reason" "..."
66 .Fn atf_get "var_name"
69 .Fn atf_require_prog "prog_name"
70 .Fn atf_set "var_name" "value"
72 .Fn atf_test_case "name" "cleanup"
75 provides a simple but powerful interface to easily write test programs in
76 the POSIX shell language.
77 These are extremely helpful given that they are trivial to write due to the
78 language simplicity and the great deal of available external tools, so they
79 are often ideal to test other applications at the user level.
81 Test programs written using this library must be run using the
83 interpreter by putting the following on their very first line:
84 .Bd -literal -offset indent
85 #! /usr/bin/env atf-sh
88 Shell-based test programs always follow this template:
89 .Bd -literal -offset indent
92 ... first test case's header ...
95 ... first test case's body ...
98 atf_test_case tc2 cleanup
100 ... second test case's header ...
103 ... second test case's body ...
106 ... second test case's cleanup ...
109 .Ns ... additional test cases ...
111 atf_init_test_cases() {
112 atf_add_test_case tc1
113 atf_add_test_case tc2
114 ... add additional test cases ...
117 .Ss Definition of test cases
118 Test cases have an identifier and are composed of three different parts:
119 the header, the body and an optional cleanup routine, all of which are
121 .Xr atf-test-case 4 .
122 To define test cases, one can use the
124 function, which takes a first parameter specifiying the test case's
125 name and instructs the library to set things up to accept it as a valid
127 The second parameter is optional and, if provided, must be
129 providing this parameter allows defining a cleanup routine for the test
131 It is important to note that this function
133 set the test case up for execution when the program is run.
134 In order to do so, a later registration is needed through the
135 .Fn atf_add_test_case
137 .Sx Program initialization .
139 Later on, one must define the three parts of the body by providing two
140 or three functions (remember that the cleanup routine is optional).
141 These functions are named after the test case's identifier, and are
146 None of these take parameters when executed.
147 .Ss Program initialization
148 The test program must define an
149 .Fn atf_init_test_cases
150 function, which is in charge of registering the test cases that will be
151 executed at run time by using the
152 .Fn atf_add_test_case
153 function, which takes the name of a test case as its single parameter.
154 This main function should not do anything else, except maybe sourcing
155 auxiliary source files that define extra variables and functions.
156 .Ss Configuration variables
157 The test case has read-only access to the current configuration variables
163 The former takes a single parameter specifying a variable name and returns
164 a boolean indicating whether the variable is defined or not.
165 The latter can take one or two parameters.
166 If it takes only one, it specifies the variable from which to get the
167 value, and this variable must be defined.
168 If it takes two, the second one specifies a default value to be returned
169 if the variable is not available.
170 .Ss Access to the source directory
171 It is possible to get the path to the test case's source directory from
172 anywhere in the test program by using the
175 It is interesting to note that this can be used inside
176 .Fn atf_init_test_cases
177 to silently include additional helper files from the source directory.
178 .Ss Requiring programs
181 meta-data variable available in the header only, one can also check for
182 additional programs in the test case's body by using the
184 function, which takes the base name or full path of a single binary.
185 Relative paths are forbidden.
186 If it is not found, the test case will be automatically skipped.
187 .Ss Test case finalization
188 The test case finalizes either when the body reaches its end, at which
189 point the test is assumed to have
191 or at any explicit call to
196 These three functions terminate the execution of the test case immediately.
197 The cleanup routine will be processed afterwards in a completely automated
198 way, regardless of the test case's termination reason.
201 does not take any parameters.
205 take a single string parameter that describes why the test case failed or
206 was skipped, respectively.
207 It is very important to provide a clear error message in both cases so that
208 the user can quickly know why the test did not pass.
210 Everything explained in the previous section changes when the test case
211 expectations are redefined by the programmer.
213 Each test case has an internal state called
215 that describes what the test case expectations are at any point in time.
216 The value of this property can change during execution by any of:
217 .Bl -tag -width indent
218 .It Fn atf_expect_death "reason" "..."
219 Expects the test case to exit prematurely regardless of the nature of the
221 .It Fn atf_expect_exit "exitcode" "reason" "..."
222 Expects the test case to exit cleanly.
227 the runtime engine will validate that the exit code of the test case
228 matches the one provided in this call.
229 Otherwise, the exact value will be ignored.
230 .It Fn atf_expect_fail "reason"
231 Any failure raised in this mode is recorded, but such failures do not report
232 the test case as failed; instead, the test case finalizes cleanly and is
234 .Sq expected failure ;
235 this report includes the provided
238 If no error is raised while running in this mode, then the test case is
242 This mode is useful to reproduce actual known bugs in tests.
243 Whenever the developer fixes the bug later on, the test case will start
244 reporting a failure, signaling the developer that the test case must be
245 adjusted to the new conditions.
246 In this situation, it is useful, for example, to set
248 as the bug number for tracking purposes.
249 .It Fn atf_expect_pass
250 This is the normal mode of execution.
251 In this mode, any failure is reported as such to the user and the test case
254 .It Fn atf_expect_signal "signo" "reason" "..."
255 Expects the test case to terminate due to the reception of a signal.
260 the runtime engine will validate that the signal that terminated the test
261 case matches the one provided in this call.
262 Otherwise, the exact value will be ignored.
263 .It Fn atf_expect_timeout "reason" "..."
264 Expects the test case to execute for longer than its timeout.
266 .Ss Helper functions for common checks
267 .Fn atf_check [options] command [args]
269 This function wraps the execution of the
271 tool and makes the test case fail if the tool reports failure.
272 You should always use this function instead of the tool in your scripts.
273 For more details on the parameters of this function, refer to
276 .Fn atf_check_equal expr1 expr2
278 This function takes two expressions, evaluates them and, if their
279 results differ, aborts the test case with an appropriate failure message.
281 The following shows a complete test program with a single test case that
282 validates the addition operator:
283 .Bd -literal -offset indent
284 atf_test_case addition
286 atf_set "descr" "Sample tests for the addition operator"
289 atf_check_equal $((0 + 0)) 0
290 atf_check_equal $((0 + 1)) 1
291 atf_check_equal $((1 + 0)) 0
293 atf_check_equal $((1 + 1)) 2
295 atf_check_equal $((100 + 200)) 300
298 atf_init_test_cases() {
299 atf_add_test_case addition
303 This other example shows how to include a file with extra helper functions
305 .Bd -literal -offset indent
306 .Ns ... definition of test cases ...
308 atf_init_test_cases() {
309 . $(atf_get_srcdir)/helper_functions.sh
311 atf_add_test_case foo1
312 atf_add_test_case foo2
316 This example demonstrates the use of the very useful
319 .Bd -literal -offset indent
320 # Check for silent output
321 atf_check -s exit:0 -o empty -e empty 'true'
323 # Check for silent output and failure
324 atf_check -s exit:1 -o empty -e empty 'false'
326 # Check for known stdout and silent stderr
328 atf_check -s exit:0 -o file:expout -e empty 'echo foo'
330 # Generate a file for later inspection
331 atf_check -s exit:0 -o save:stdout -e empty 'ls'
332 grep foo ls || atf_fail "foo file not found in listing"
334 # Or just do the match along the way
335 atf_check -s exit:0 -o match:"^foo$" -e empty 'ls'
339 .Xr atf-test-program 1 ,