]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/atf/atf-sh/atf-sh-api.3
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / atf / atf-sh / atf-sh-api.3
1 .\"
2 .\" Automated Testing Framework (atf)
3 .\"
4 .\" Copyright (c) 2008 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-SH-API 3
31 .Os
32 .Sh NAME
33 .Nm atf_add_test_case ,
34 .Nm atf_check ,
35 .Nm atf_check_equal ,
36 .Nm atf_config_get ,
37 .Nm atf_config_has ,
38 .Nm atf_expect_death ,
39 .Nm atf_expect_exit ,
40 .Nm atf_expect_fail ,
41 .Nm atf_expect_pass ,
42 .Nm atf_expect_signal ,
43 .Nm atf_expect_timeout ,
44 .Nm atf_fail ,
45 .Nm atf_get ,
46 .Nm atf_get_srcdir ,
47 .Nm atf_pass ,
48 .Nm atf_require_prog ,
49 .Nm atf_set ,
50 .Nm atf_skip ,
51 .Nm atf_test_case
52 .Nd POSIX shell API to write ATF-based test programs
53 .Sh SYNOPSIS
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" "..."
62 .Fn atf_expect_pass
63 .Fn atf_expect_signal "signo" "reason" "..."
64 .Fn atf_expect_timeout "reason" "..."
65 .Fn atf_fail "reason"
66 .Fn atf_get "var_name"
67 .Fn atf_get_srcdir
68 .Fn atf_pass
69 .Fn atf_require_prog "prog_name"
70 .Fn atf_set "var_name" "value"
71 .Fn atf_skip "reason"
72 .Fn atf_test_case "name" "cleanup"
73 .Sh DESCRIPTION
74 ATF
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.
80 .Pp
81 Test programs written using this library must be run using the
82 .Xr atf-sh 1
83 interpreter by putting the following on their very first line:
84 .Bd -literal -offset indent
85 #! /usr/bin/env atf-sh
86 .Ed
87 .Pp
88 Shell-based test programs always follow this template:
89 .Bd -literal -offset indent
90 atf_test_case tc1
91 tc1_head() {
92     ... first test case's header ...
93 }
94 tc1_body() {
95     ... first test case's body ...
96 }
97
98 atf_test_case tc2 cleanup
99 tc2_head() {
100     ... second test case's header ...
101 }
102 tc2_body() {
103     ... second test case's body ...
104 }
105 tc2_cleanup() {
106     ... second test case's cleanup ...
107 }
108
109 .Ns ... additional test cases ...
110
111 atf_init_test_cases() {
112     atf_add_test_case tc1
113     atf_add_test_case tc2
114     ... add additional test cases ...
115 }
116 .Ed
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
120 described in
121 .Xr atf-test-case 4 .
122 To define test cases, one can use the
123 .Fn atf_test_case
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
126 test case.
127 The second parameter is optional and, if provided, must be
128 .Sq cleanup ;
129 providing this parameter allows defining a cleanup routine for the test
130 case.
131 It is important to note that this function
132 .Em does not
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
136 function detailed in
137 .Sx Program initialization .
138 .Pp
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
142 .Fn <id>_head ,
143 .Fn <id>_body
144 and
145 .Fn <id>_cleanup.
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
158 through the
159 .Fn atf_config_has
160 and
161 .Fn atf_config_get
162 methods.
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
173 .Fn atf_get_srcdir
174 function.
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
179 Aside from the
180 .Va require.progs
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
183 .Fn atf_require_prog
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
190 .Em passed ,
191 or at any explicit call to
192 .Fn atf_pass ,
193 .Fn atf_fail
194 or
195 .Fn atf_skip .
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.
199 .Pp
200 .Fn atf_pass
201 does not take any parameters.
202 .Fn atf_fail
203 and
204 .Fn atf_skip
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.
209 .Ss Expectations
210 Everything explained in the previous section changes when the test case
211 expectations are redefined by the programmer.
212 .Pp
213 Each test case has an internal state called
214 .Sq expect
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
220 exit.
221 .It Fn atf_expect_exit "exitcode" "reason" "..."
222 Expects the test case to exit cleanly.
223 If
224 .Va exitcode
225 is not
226 .Sq -1 ,
227 .Xr atf-run 1
228 will validate that the exit code of the test case matches the one provided
229 in this call.
230 Otherwise, the exact value will be ignored.
231 .It Fn atf_expect_fail "reason"
232 Any failure raised in this mode is recorded, but such failures do not report
233 the test case as failed; instead, the test case finalizes cleanly and is
234 reported as
235 .Sq expected failure ;
236 this report includes the provided
237 .Fa reason
238 as part of it.
239 If no error is raised while running in this mode, then the test case is
240 reported as
241 .Sq failed .
242 .Pp
243 This mode is useful to reproduce actual known bugs in tests.
244 Whenever the developer fixes the bug later on, the test case will start
245 reporting a failure, signaling the developer that the test case must be
246 adjusted to the new conditions.
247 In this situation, it is useful, for example, to set
248 .Fa reason
249 as the bug number for tracking purposes.
250 .It Fn atf_expect_pass
251 This is the normal mode of execution.
252 In this mode, any failure is reported as such to the user and the test case
253 is marked as
254 .Sq failed .
255 .It Fn atf_expect_signal "signo" "reason" "..."
256 Expects the test case to terminate due to the reception of a signal.
257 If
258 .Va signo
259 is not
260 .Sq -1 ,
261 .Xr atf-run 1
262 will validate that the signal that terminated the test case matches the one
263 provided in this call.
264 Otherwise, the exact value will be ignored.
265 .It Fn atf_expect_timeout "reason" "..."
266 Expects the test case to execute for longer than its timeout.
267 .El
268 .Ss Helper functions for common checks
269 .Fn atf_check [options] command [args]
270 .Pp
271 This function wraps the execution of the
272 .Nm atf-check
273 tool and makes the test case fail if the tool reports failure.
274 You should always use this function instead of the tool in your scripts.
275 For more details on the parameters of this function, refer to
276 .Xr atf-check 1 .
277 .Pp
278 .Fn atf_check_equal expr1 expr2
279 .Pp
280 This function takes two expressions, evaluates them and, if their
281 results differ, aborts the test case with an appropriate failure message.
282 .Sh EXAMPLES
283 The following shows a complete test program with a single test case that
284 validates the addition operator:
285 .Bd -literal -offset indent
286 atf_test_case addition
287 addition_head() {
288     atf_set "descr" "Sample tests for the addition operator"
289 }
290 addition_body() {
291     atf_check_equal $((0 + 0)) 0
292     atf_check_equal $((0 + 1)) 1
293     atf_check_equal $((1 + 0)) 0
294
295     atf_check_equal $((1 + 1)) 2
296
297     atf_check_equal $((100 + 200)) 300
298 }
299
300 atf_init_test_cases() {
301     atf_add_test_case addition
302 }
303 .Ed
304 .Pp
305 This other example shows how to include a file with extra helper functions
306 in the test program:
307 .Bd -literal -offset indent
308 .Ns ... definition of test cases ...
309
310 atf_init_test_cases() {
311     . $(atf_get_srcdir)/helper_functions.sh
312
313     atf_add_test_case foo1
314     atf_add_test_case foo2
315 }
316 .Ed
317 .Pp
318 This example demonstrates the use of the very useful
319 .Fn atf_check
320 function:
321 .Bd -literal -offset indent
322 # Check for silent output
323 atf_check 'true' 0 null null
324
325 # Check for silent output and failure
326 atf_check 'false' 1 null null
327
328 # Check for known stdout and silent stderr
329 echo foo >expout
330 atf_check 'echo foo' 0 expout null
331
332 # Generate a file for later inspection
333 atf_check 'ls' 0 stdout null
334 grep foo ls || atf_fail "foo file not found in listing"
335 .Ed
336 .Sh SEE ALSO
337 .Xr atf-sh 1 ,
338 .Xr atf-test-program 1 ,
339 .Xr atf-test-case 4 ,
340 .Xr atf 7