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.
33 .Nm atf_add_test_case ,
42 .Nm atf_require_prog ,
45 .Nd POSIX shell API to write ATF-based test programs
47 .Fn atf_add_test_case "name"
48 .Fn atf_check "command"
49 .Fn atf_check_equal "expr1" "expr2"
50 .Fn atf_config_get "var_name"
51 .Fn atf_config_has "var_name"
53 .Fn atf_get "var_name"
56 .Fn atf_require_prog "prog_name"
57 .Fn atf_set "var_name" "value"
61 provides a simple but powerful interface to easily write test programs in
62 the POSIX shell language.
63 These are extremely helpful given that they are trivial to write due to the
64 language simplicity and the great deal of available external tools, so they
65 are often ideal to test other applications at the user level.
67 Test programs written using this library must be preprocessed by the
69 tool, which includes some boilerplate code and generates the final
70 (installable) test program.
72 Shell-based test programs always follow this template:
73 .Bd -literal -offset indent
76 ... first test case's header ...
79 ... first test case's body ...
84 ... second test case's header ...
87 ... second test case's body ...
90 ... second test case's cleanup ...
93 .Ns ... additional test cases ...
95 atf_init_test_cases() {
98 ... add additional test cases ...
101 .Ss Definition of test cases
102 Test cases have an identifier and are composed of three different parts:
103 the header, the body and an optional cleanup routine, all of which are
105 .Xr atf-test-case 8 .
106 To define test cases, one can use the
108 function, which takes a single parameter specifiying the test case's
109 name and instructs the library to set things up to accept it as a valid
111 It is important to note that it
113 set the test case up for execution when the program is run.
114 In order to do so, a later registration is needed through the
115 .Fn atf_add_test_case
117 .Sx Program initialization .
119 Later on, one must define the three parts of the body by providing two
120 or three functions (remember that the cleanup routine is optional).
121 These functions are named after the test case's identifier, and are
126 None of these take parameters when executed.
127 .Ss Program initialization
128 The test program must define an
129 .Fn atf_init_test_cases
130 function, which is in charge of registering the test cases that will be
131 executed at run time by using the
132 .Fn atf_add_test_case
133 function, which takes the name of a test case as its single parameter.
134 This main function should not do anything else, except maybe sourcing
135 auxiliary source files that define extra variables and functions.
136 .Ss Configuration variables
137 The test case has read-only access to the current configuration variables
143 The former takes a single parameter specifying a variable name and returns
144 a boolean indicating whether the variable is defined or not.
145 The latter can take one or two parameters.
146 If it takes only one, it specifies the variable from which to get the
147 value, and this variable must be defined.
148 If it takes two, the second one specifies a default value to be returned
149 if the variable is not available.
150 .Ss Access to the source directory
151 It is possible to get the path to the test case's source directory from
152 anywhere in the test program by using the
155 It is interesting to note that this can be used inside
156 .Fn atf_init_test_cases
157 to silently include additional helper files from the source directory.
158 .Ss Requiring programs
161 meta-data variable available in the header only, one can also check for
162 additional programs in the test case's body by using the
164 function, which takes the base name or full path of a single binary.
165 Relative paths are forbidden.
166 If it is not found, the test case will be automatically skipped.
167 .Ss Test case finalization
168 The test case finalizes either when the body reaches its end, at which
169 point the test is assumed to have
171 or at any explicit call to
176 These three functions terminate the execution of the test case immediately.
177 The cleanup routine will be processed afterwards in a completely automated
178 way, regardless of the test case's termination reason.
181 does not take any parameters.
185 take a single string parameter that describes why the test case failed or
186 was skipped, respectively.
187 It is very important to provide a clear error message in both cases so that
188 the user can quickly know why the test did not pass.
189 .Ss Helper functions for common checks
190 .Fn atf_check [options] command [args]
192 This function wraps the execution of the
194 tool and makes the test case fail if the tool reports failure.
195 You should always use this function instead of the tool in your scripts.
196 For more details on the parameters of this function, refer to
199 .Fn atf_check_equal expr1 expr2
201 This function takes two expressions, evaluates them and, if their
202 results differ, aborts the test case with an appropriate failure message.
204 The following shows a complete test program with a single test case that
205 validates the addition operator:
206 .Bd -literal -offset indent
207 atf_test_case addition
209 atf_set "descr" "Sample tests for the addition operator"
212 atf_check_equal $((0 + 0)) 0
213 atf_check_equal $((0 + 1)) 1
214 atf_check_equal $((1 + 0)) 0
216 atf_check_equal $((1 + 1)) 2
218 atf_check_equal $((100 + 200)) 300
221 atf_init_test_cases() {
222 atf_add_test_case addition
226 This other example shows how to include a file with extra helper functions
228 .Bd -literal -offset indent
229 .Ns ... definition of test cases ...
231 atf_init_test_cases() {
232 . $(atf_get_srcdir)/helper_functions.sh
234 atf_add_test_case foo1
235 atf_add_test_case foo2
239 This example demonstrates the use of the very useful
242 .Bd -literal -offset indent
243 # Check for silent output
244 atf_check 'true' 0 null null
246 # Check for silent output and failure
247 atf_check 'false' 1 null null
249 # Check for known stdout and silent stderr
251 atf_check 'echo foo' 0 expout null
253 # Generate a file for later inspection
254 atf_check 'ls' 0 stdout null
255 grep foo ls || atf_fail "foo file not found in listing"
259 .Xr atf-test-program 1 ,