1.\"	$NetBSD: atf-sh-api.3,v 1.3 2014/12/10 04:38:03 christos Exp $
2.\"
3.\"
4.\" Automated Testing Framework (atf)
5.\"
6.\" Copyright (c) 2008 The NetBSD Foundation, Inc.
7.\" All rights reserved.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\"
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.
30.\"
31.Dd June 28, 2010
32.Dt ATF-SH-API 3
33.Os
34.Sh NAME
35.Nm atf_add_test_case ,
36.Nm atf_check ,
37.Nm atf_check_equal ,
38.Nm atf_config_get ,
39.Nm atf_config_has ,
40.Nm atf_expect_death ,
41.Nm atf_expect_exit ,
42.Nm atf_expect_fail ,
43.Nm atf_expect_pass ,
44.Nm atf_expect_signal ,
45.Nm atf_expect_timeout ,
46.Nm atf_fail ,
47.Nm atf_get ,
48.Nm atf_get_srcdir ,
49.Nm atf_pass ,
50.Nm atf_require_prog ,
51.Nm atf_set ,
52.Nm atf_skip ,
53.Nm atf_test_case
54.Nd POSIX shell API to write ATF-based test programs
55.Sh SYNOPSIS
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" "..."
64.Fn atf_expect_pass
65.Fn atf_expect_signal "signo" "reason" "..."
66.Fn atf_expect_timeout "reason" "..."
67.Fn atf_fail "reason"
68.Fn atf_get "var_name"
69.Fn atf_get_srcdir
70.Fn atf_pass
71.Fn atf_require_prog "prog_name"
72.Fn atf_set "var_name" "value"
73.Fn atf_skip "reason"
74.Fn atf_test_case "name" "cleanup"
75.Sh DESCRIPTION
76ATF
77provides a simple but powerful interface to easily write test programs in
78the POSIX shell language.
79These are extremely helpful given that they are trivial to write due to the
80language simplicity and the great deal of available external tools, so they
81are often ideal to test other applications at the user level.
82.Pp
83Test programs written using this library must be run using the
84.Xr atf-sh 1
85interpreter by putting the following on their very first line:
86.Bd -literal -offset indent
87#! /usr/bin/env atf-sh
88.Ed
89.Pp
90Shell-based test programs always follow this template:
91.Bd -literal -offset indent
92atf_test_case tc1
93tc1_head() {
94    ... first test case's header ...
95}
96tc1_body() {
97    ... first test case's body ...
98}
99
100atf_test_case tc2 cleanup
101tc2_head() {
102    ... second test case's header ...
103}
104tc2_body() {
105    ... second test case's body ...
106}
107tc2_cleanup() {
108    ... second test case's cleanup ...
109}
110
111.Ns ... additional test cases ...
112
113atf_init_test_cases() {
114    atf_add_test_case tc1
115    atf_add_test_case tc2
116    ... add additional test cases ...
117}
118.Ed
119.Ss Definition of test cases
120Test cases have an identifier and are composed of three different parts:
121the header, the body and an optional cleanup routine, all of which are
122described in
123.Xr atf-test-case 4 .
124To define test cases, one can use the
125.Fn atf_test_case
126function, which takes a first parameter specifiying the test case's
127name and instructs the library to set things up to accept it as a valid
128test case.
129The second parameter is optional and, if provided, must be
130.Sq cleanup ;
131providing this parameter allows defining a cleanup routine for the test
132case.
133It is important to note that this function
134.Em does not
135set the test case up for execution when the program is run.
136In order to do so, a later registration is needed through the
137.Fn atf_add_test_case
138function detailed in
139.Sx Program initialization .
140.Pp
141Later on, one must define the three parts of the body by providing two
142or three functions (remember that the cleanup routine is optional).
143These functions are named after the test case's identifier, and are
144.Fn <id>_head ,
145.Fn <id>_body
146and
147.Fn <id>_cleanup.
148None of these take parameters when executed.
149.Ss Program initialization
150The test program must define an
151.Fn atf_init_test_cases
152function, which is in charge of registering the test cases that will be
153executed at run time by using the
154.Fn atf_add_test_case
155function, which takes the name of a test case as its single parameter.
156This main function should not do anything else, except maybe sourcing
157auxiliary source files that define extra variables and functions.
158.Ss Configuration variables
159The test case has read-only access to the current configuration variables
160through the
161.Fn atf_config_has
162and
163.Fn atf_config_get
164methods.
165The former takes a single parameter specifying a variable name and returns
166a boolean indicating whether the variable is defined or not.
167The latter can take one or two parameters.
168If it takes only one, it specifies the variable from which to get the
169value, and this variable must be defined.
170If it takes two, the second one specifies a default value to be returned
171if the variable is not available.
172.Ss Access to the source directory
173It is possible to get the path to the test case's source directory from
174anywhere in the test program by using the
175.Fn atf_get_srcdir
176function.
177It is interesting to note that this can be used inside
178.Fn atf_init_test_cases
179to silently include additional helper files from the source directory.
180.Ss Requiring programs
181Aside from the
182.Va require.progs
183meta-data variable available in the header only, one can also check for
184additional programs in the test case's body by using the
185.Fn atf_require_prog
186function, which takes the base name or full path of a single binary.
187Relative paths are forbidden.
188If it is not found, the test case will be automatically skipped.
189.Ss Test case finalization
190The test case finalizes either when the body reaches its end, at which
191point the test is assumed to have
192.Em passed ,
193or at any explicit call to
194.Fn atf_pass ,
195.Fn atf_fail
196or
197.Fn atf_skip .
198These three functions terminate the execution of the test case immediately.
199The cleanup routine will be processed afterwards in a completely automated
200way, regardless of the test case's termination reason.
201.Pp
202.Fn atf_pass
203does not take any parameters.
204.Fn atf_fail
205and
206.Fn atf_skip
207take a single string parameter that describes why the test case failed or
208was skipped, respectively.
209It is very important to provide a clear error message in both cases so that
210the user can quickly know why the test did not pass.
211.Ss Expectations
212Everything explained in the previous section changes when the test case
213expectations are redefined by the programmer.
214.Pp
215Each test case has an internal state called
216.Sq expect
217that describes what the test case expectations are at any point in time.
218The value of this property can change during execution by any of:
219.Bl -tag -width indent
220.It Fn atf_expect_death "reason" "..."
221Expects the test case to exit prematurely regardless of the nature of the
222exit.
223.It Fn atf_expect_exit "exitcode" "reason" "..."
224Expects the test case to exit cleanly.
225If
226.Va exitcode
227is not
228.Sq -1 ,
229.Xr atf-run 1
230will validate that the exit code of the test case matches the one provided
231in this call.
232Otherwise, the exact value will be ignored.
233.It Fn atf_expect_fail "reason"
234Any failure raised in this mode is recorded, but such failures do not report
235the test case as failed; instead, the test case finalizes cleanly and is
236reported as
237.Sq expected failure ;
238this report includes the provided
239.Fa reason
240as part of it.
241If no error is raised while running in this mode, then the test case is
242reported as
243.Sq failed .
244.Pp
245This mode is useful to reproduce actual known bugs in tests.
246Whenever the developer fixes the bug later on, the test case will start
247reporting a failure, signaling the developer that the test case must be
248adjusted to the new conditions.
249In this situation, it is useful, for example, to set
250.Fa reason
251as the bug number for tracking purposes.
252.It Fn atf_expect_pass
253This is the normal mode of execution.
254In this mode, any failure is reported as such to the user and the test case
255is marked as
256.Sq failed .
257.It Fn atf_expect_signal "signo" "reason" "..."
258Expects the test case to terminate due to the reception of a signal.
259If
260.Va signo
261is not
262.Sq -1 ,
263.Xr atf-run 1
264will validate that the signal that terminated the test case matches the one
265provided in this call.
266Otherwise, the exact value will be ignored.
267.It Fn atf_expect_timeout "reason" "..."
268Expects the test case to execute for longer than its timeout.
269.El
270.Ss Helper functions for common checks
271.Fn atf_check [options] command [args]
272.Pp
273This function wraps the execution of the
274.Nm atf-check
275tool and makes the test case fail if the tool reports failure.
276You should always use this function instead of the tool in your scripts.
277For more details on the parameters of this function, refer to
278.Xr atf-check 1 .
279.Pp
280.Fn atf_check_equal expr1 expr2
281.Pp
282This function takes two expressions, evaluates them and, if their
283results differ, aborts the test case with an appropriate failure message.
284.Sh EXAMPLES
285The following shows a complete test program with a single test case that
286validates the addition operator:
287.Bd -literal -offset indent
288atf_test_case addition
289addition_head() {
290    atf_set "descr" "Sample tests for the addition operator"
291}
292addition_body() {
293    atf_check_equal $((0 + 0)) 0
294    atf_check_equal $((0 + 1)) 1
295    atf_check_equal $((1 + 0)) 0
296
297    atf_check_equal $((1 + 1)) 2
298
299    atf_check_equal $((100 + 200)) 300
300}
301
302atf_init_test_cases() {
303    atf_add_test_case addition
304}
305.Ed
306.Pp
307This other example shows how to include a file with extra helper functions
308in the test program:
309.Bd -literal -offset indent
310.Ns ... definition of test cases ...
311
312atf_init_test_cases() {
313    . $(atf_get_srcdir)/helper_functions.sh
314
315    atf_add_test_case foo1
316    atf_add_test_case foo2
317}
318.Ed
319.Pp
320This example demonstrates the use of the very useful
321.Fn atf_check
322function:
323.Bd -literal -offset indent
324# Check for silent output
325atf_check 'true' 0 null null
326
327# Check for silent output and failure
328atf_check 'false' 1 null null
329
330# Check for known stdout and silent stderr
331echo foo >expout
332atf_check 'echo foo' 0 expout null
333
334# Generate a file for later inspection
335atf_check 'ls' 0 stdout null
336grep foo ls || atf_fail "foo file not found in listing"
337.Ed
338.Sh SEE ALSO
339.Xr atf-sh 1 ,
340.Xr atf-test-program 1 ,
341.Xr atf-test-case 4 ,
342.Xr atf 7
343