1 .\" $NetBSD: atf-sh-api.3,v 1.3 2014/12/10 04:38:03 christos Exp $
4 .\" Automated Testing Framework (atf)
6 .\" Copyright (c) 2008 The NetBSD Foundation, Inc.
7 .\" All rights reserved.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 .\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 .\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 .\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 .\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 .Nm atf_add_test_case ,
40 .Nm atf_expect_death ,
44 .Nm atf_expect_signal ,
45 .Nm atf_expect_timeout ,
50 .Nm atf_require_prog ,
54 .Nd POSIX shell API to write ATF-based test programs
56 .Fn atf_add_test_case "name"
57 .Fn atf_check "command"
58 .Fn atf_check_equal "expr1" "expr2"
59 .Fn atf_config_get "var_name"
60 .Fn atf_config_has "var_name"
61 .Fn atf_expect_death "reason" "..."
62 .Fn atf_expect_exit "exitcode" "reason" "..."
63 .Fn atf_expect_fail "reason" "..."
65 .Fn atf_expect_signal "signo" "reason" "..."
66 .Fn atf_expect_timeout "reason" "..."
68 .Fn atf_get "var_name"
71 .Fn atf_require_prog "prog_name"
72 .Fn atf_set "var_name" "value"
74 .Fn atf_test_case "name" "cleanup"
77 provides a simple but powerful interface to easily write test programs in
78 the POSIX shell language.
79 These are extremely helpful given that they are trivial to write due to the
80 language simplicity and the great deal of available external tools, so they
81 are often ideal to test other applications at the user level.
83 Test programs written using this library must be run using the
85 interpreter by putting the following on their very first line:
86 .Bd -literal -offset indent
87 #! /usr/bin/env atf-sh
90 Shell-based test programs always follow this template:
91 .Bd -literal -offset indent
94 ... first test case's header ...
97 ... first test case's body ...
100 atf_test_case tc2 cleanup
102 ... second test case's header ...
105 ... second test case's body ...
108 ... second test case's cleanup ...
111 .Ns ... additional test cases ...
113 atf_init_test_cases() {
114 atf_add_test_case tc1
115 atf_add_test_case tc2
116 ... add additional test cases ...
119 .Ss Definition of test cases
120 Test cases have an identifier and are composed of three different parts:
121 the header, the body and an optional cleanup routine, all of which are
123 .Xr atf-test-case 4 .
124 To define test cases, one can use the
126 function, which takes a first parameter specifiying the test case's
127 name and instructs the library to set things up to accept it as a valid
129 The second parameter is optional and, if provided, must be
131 providing this parameter allows defining a cleanup routine for the test
133 It is important to note that this function
135 set the test case up for execution when the program is run.
136 In order to do so, a later registration is needed through the
137 .Fn atf_add_test_case
139 .Sx Program initialization .
141 Later on, one must define the three parts of the body by providing two
142 or three functions (remember that the cleanup routine is optional).
143 These functions are named after the test case's identifier, and are
148 None of these take parameters when executed.
149 .Ss Program initialization
150 The test program must define an
151 .Fn atf_init_test_cases
152 function, which is in charge of registering the test cases that will be
153 executed at run time by using the
154 .Fn atf_add_test_case
155 function, which takes the name of a test case as its single parameter.
156 This main function should not do anything else, except maybe sourcing
157 auxiliary source files that define extra variables and functions.
158 .Ss Configuration variables
159 The test case has read-only access to the current configuration variables
165 The former takes a single parameter specifying a variable name and returns
166 a boolean indicating whether the variable is defined or not.
167 The latter can take one or two parameters.
168 If it takes only one, it specifies the variable from which to get the
169 value, and this variable must be defined.
170 If it takes two, the second one specifies a default value to be returned
171 if the variable is not available.
172 .Ss Access to the source directory
173 It is possible to get the path to the test case's source directory from
174 anywhere in the test program by using the
177 It is interesting to note that this can be used inside
178 .Fn atf_init_test_cases
179 to silently include additional helper files from the source directory.
180 .Ss Requiring programs
183 meta-data variable available in the header only, one can also check for
184 additional programs in the test case's body by using the
186 function, which takes the base name or full path of a single binary.
187 Relative paths are forbidden.
188 If it is not found, the test case will be automatically skipped.
189 .Ss Test case finalization
190 The test case finalizes either when the body reaches its end, at which
191 point the test is assumed to have
193 or at any explicit call to
198 These three functions terminate the execution of the test case immediately.
199 The cleanup routine will be processed afterwards in a completely automated
200 way, regardless of the test case's termination reason.
203 does not take any parameters.
207 take a single string parameter that describes why the test case failed or
208 was skipped, respectively.
209 It is very important to provide a clear error message in both cases so that
210 the user can quickly know why the test did not pass.
212 Everything explained in the previous section changes when the test case
213 expectations are redefined by the programmer.
215 Each test case has an internal state called
217 that describes what the test case expectations are at any point in time.
218 The value of this property can change during execution by any of:
219 .Bl -tag -width indent
220 .It Fn atf_expect_death "reason" "..."
221 Expects the test case to exit prematurely regardless of the nature of the
223 .It Fn atf_expect_exit "exitcode" "reason" "..."
224 Expects the test case to exit cleanly.
230 will validate that the exit code of the test case matches the one provided
232 Otherwise, the exact value will be ignored.
233 .It Fn atf_expect_fail "reason"
234 Any failure raised in this mode is recorded, but such failures do not report
235 the test case as failed; instead, the test case finalizes cleanly and is
237 .Sq expected failure ;
238 this report includes the provided
241 If no error is raised while running in this mode, then the test case is
245 This mode is useful to reproduce actual known bugs in tests.
246 Whenever the developer fixes the bug later on, the test case will start
247 reporting a failure, signaling the developer that the test case must be
248 adjusted to the new conditions.
249 In this situation, it is useful, for example, to set
251 as the bug number for tracking purposes.
252 .It Fn atf_expect_pass
253 This is the normal mode of execution.
254 In this mode, any failure is reported as such to the user and the test case
257 .It Fn atf_expect_signal "signo" "reason" "..."
258 Expects the test case to terminate due to the reception of a signal.
264 will validate that the signal that terminated the test case matches the one
265 provided in this call.
266 Otherwise, the exact value will be ignored.
267 .It Fn atf_expect_timeout "reason" "..."
268 Expects the test case to execute for longer than its timeout.
270 .Ss Helper functions for common checks
271 .Fn atf_check [options] command [args]
273 This function wraps the execution of the
275 tool and makes the test case fail if the tool reports failure.
276 You should always use this function instead of the tool in your scripts.
277 For more details on the parameters of this function, refer to
280 .Fn atf_check_equal expr1 expr2
282 This function takes two expressions, evaluates them and, if their
283 results differ, aborts the test case with an appropriate failure message.
285 The following shows a complete test program with a single test case that
286 validates the addition operator:
287 .Bd -literal -offset indent
288 atf_test_case addition
290 atf_set "descr" "Sample tests for the addition operator"
293 atf_check_equal $((0 + 0)) 0
294 atf_check_equal $((0 + 1)) 1
295 atf_check_equal $((1 + 0)) 0
297 atf_check_equal $((1 + 1)) 2
299 atf_check_equal $((100 + 200)) 300
302 atf_init_test_cases() {
303 atf_add_test_case addition
307 This other example shows how to include a file with extra helper functions
309 .Bd -literal -offset indent
310 .Ns ... definition of test cases ...
312 atf_init_test_cases() {
313 . $(atf_get_srcdir)/helper_functions.sh
315 atf_add_test_case foo1
316 atf_add_test_case foo2
320 This example demonstrates the use of the very useful
323 .Bd -literal -offset indent
324 # Check for silent output
325 atf_check 'true' 0 null null
327 # Check for silent output and failure
328 atf_check 'false' 1 null null
330 # Check for known stdout and silent stderr
332 atf_check 'echo foo' 0 expout null
334 # Generate a file for later inspection
335 atf_check 'ls' 0 stdout null
336 grep foo ls || atf_fail "foo file not found in listing"
340 .Xr atf-test-program 1 ,
341 .Xr atf-test-case 4 ,