xref: /netbsd-src/external/bsd/atf/dist/atf-sh/atf-sh-api.3 (revision 27fd3f6531803adac12382d7643a9a492b576601)
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 February 29, 2008
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_fail ,
39.Nm atf_get ,
40.Nm atf_get_srcdir ,
41.Nm atf_pass ,
42.Nm atf_require_prog ,
43.Nm atf_set ,
44.Nm atf_skip
45.Nd POSIX shell API to write ATF-based test programs
46.Sh SYNOPSIS
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"
52.Fn atf_fail "reason"
53.Fn atf_get "var_name"
54.Fn atf_get_srcdir
55.Fn atf_pass
56.Fn atf_require_prog "prog_name"
57.Fn atf_set "var_name" "value"
58.Fn atf_skip "reason"
59.Sh DESCRIPTION
60ATF
61provides a simple but powerful interface to easily write test programs in
62the POSIX shell language.
63These are extremely helpful given that they are trivial to write due to the
64language simplicity and the great deal of available external tools, so they
65are often ideal to test other applications at the user level.
66.Pp
67Test programs written using this library must be preprocessed by the
68.Xr atf-compile 1
69tool, which includes some boilerplate code and generates the final
70(installable) test program.
71.Pp
72Shell-based test programs always follow this template:
73.Bd -literal -offset indent
74atf_test_case tc1
75tc1_head() {
76    ... first test case's header ...
77}
78tc1_body() {
79    ... first test case's body ...
80}
81
82atf_test_case tc2
83tc2_head() {
84    ... second test case's header ...
85}
86tc2_body() {
87    ... second test case's body ...
88}
89tc2_cleanup() {
90    ... second test case's cleanup ...
91}
92
93.Ns ... additional test cases ...
94
95atf_init_test_cases() {
96    atf_add_test_case tc1
97    atf_add_test_case tc2
98    ... add additional test cases ...
99}
100.Ed
101.Ss Definition of test cases
102Test cases have an identifier and are composed of three different parts:
103the header, the body and an optional cleanup routine, all of which are
104described in
105.Xr atf-test-case 8 .
106To define test cases, one can use the
107.Fn atf_test_case
108function, which takes a single parameter specifiying the test case's
109name and instructs the library to set things up to accept it as a valid
110test case.
111It is important to note that it
112.Em does not
113set the test case up for execution when the program is run.
114In order to do so, a later registration is needed through the
115.Fn atf_add_test_case
116function detailed in
117.Sx Program initialization .
118.Pp
119Later on, one must define the three parts of the body by providing two
120or three functions (remember that the cleanup routine is optional).
121These functions are named after the test case's identifier, and are
122.Fn <id>_head ,
123.Fn <id>_body
124and
125.Fn <id>_cleanup.
126None of these take parameters when executed.
127.Ss Program initialization
128The test program must define an
129.Fn atf_init_test_cases
130function, which is in charge of registering the test cases that will be
131executed at run time by using the
132.Fn atf_add_test_case
133function, which takes the name of a test case as its single parameter.
134This main function should not do anything else, except maybe sourcing
135auxiliary source files that define extra variables and functions.
136.Ss Configuration variables
137The test case has read-only access to the current configuration variables
138through the
139.Fn atf_config_has
140and
141.Fn atf_config_get
142methods.
143The former takes a single parameter specifying a variable name and returns
144a boolean indicating whether the variable is defined or not.
145The latter can take one or two parameters.
146If it takes only one, it specifies the variable from which to get the
147value, and this variable must be defined.
148If it takes two, the second one specifies a default value to be returned
149if the variable is not available.
150.Ss Access to the source directory
151It is possible to get the path to the test case's source directory from
152anywhere in the test program by using the
153.Fn atf_get_srcdir
154function.
155It is interesting to note that this can be used inside
156.Fn atf_init_test_cases
157to silently include additional helper files from the source directory.
158.Ss Requiring programs
159Aside from the
160.Va require.progs
161meta-data variable available in the header only, one can also check for
162additional programs in the test case's body by using the
163.Fn atf_require_prog
164function, which takes the base name or full path of a single binary.
165Relative paths are forbidden.
166If it is not found, the test case will be automatically skipped.
167.Ss Test case finalization
168The test case finalizes either when the body reaches its end, at which
169point the test is assumed to have
170.Em passed ,
171or at any explicit call to
172.Fn atf_pass ,
173.Fn atf_fail
174or
175.Fn atf_skip .
176These three functions terminate the execution of the test case immediately.
177The cleanup routine will be processed afterwards in a completely automated
178way, regardless of the test case's termination reason.
179.Pp
180.Fn atf_pass
181does not take any parameters.
182.Fn atf_fail
183and
184.Fn atf_skip
185take a single string parameter that describes why the test case failed or
186was skipped, respectively.
187It is very important to provide a clear error message in both cases so that
188the user can quickly know why the test did not pass.
189.Ss Helper functions for common checks
190.Fn atf_check cmd expcode expout experr
191.Pp
192This function takes four parameters: the command to execute, the expected
193numerical exit code, the expected behavior of
194.Dv stdout
195and the expected
196behavior of
197.Dv stderr .
198.Fa expout
199can be one of the following:
200.Bl -tag -width expoutXX
201.It Li expout
202What the command writes to the
203.Dv stdout
204channel must match exactly what is
205found in the
206.Pa expout
207file.
208.It Li ignore
209The test does not check what the command writes to the
210.Dv stdout
211channel.
212.It Li null
213The command must not write anything to the
214.Dv stdout
215channel.
216.It Li stdout
217What the command writes to the
218.Dv stdout
219channel is written to a
220.Pa stdout
221file, available for further inspection.
222.El
223.Pp
224Similarly,
225.Fa experr
226can be one of
227.Sq Li experr ,
228.Sq Li ignore ,
229.Sq Li null ,
230or
231.Sq Li stderr ,
232all of which follow the same semantics of their corresponding counterparts
233for the
234.Fa expout
235case.
236.Pp
237It is important to note that when a failure is detected, this function will
238print as much information as possible to be able to identify the cause of
239the failure.
240For example, if the
241.Dv stdout
242does not match with the expected contents, a
243diff will be printed.
244.Pp
245.Fn atf_check_equal expr1 expr2
246.Pp
247This function takes two expressions, evaluates them and, if their
248results differ, aborts the test case with an appropriate failure message.
249.Sh EXAMPLES
250The following shows a complete test program with a single test case that
251validates the addition operator:
252.Bd -literal -offset indent
253atf_test_case addition
254addition_head() {
255    atf_set "descr" "Sample tests for the addition operator"
256}
257addition_body() {
258    atf_check_equal $((0 + 0)) 0
259    atf_check_equal $((0 + 1)) 1
260    atf_check_equal $((1 + 0)) 0
261
262    atf_check_equal $((1 + 1)) 2
263
264    atf_check_equal $((100 + 200)) 300
265}
266
267atf_init_test_cases() {
268    atf_add_test_case addition
269}
270.Ed
271.Pp
272This other example shows how to include a file with extra helper functions
273in the test program:
274.Bd -literal -offset indent
275.Ns ... definition of test cases ...
276
277atf_init_test_cases() {
278    . $(atf_get_srcdir)/helper_functions.sh
279
280    atf_add_test_case foo1
281    atf_add_test_case foo2
282}
283.Ed
284.Pp
285This example demonstrates the use of the very useful
286.Fn atf_check
287function:
288.Bd -literal -offset indent
289# Check for silent output
290atf_check 'true' 0 null null
291
292# Check for silent output and failure
293atf_check 'false' 1 null null
294
295# Check for known stdout and silent stderr
296echo foo >expout
297atf_check 'echo foo' 0 expout null
298
299# Generate a file for later inspection
300atf_check 'ls' 0 stdout null
301grep foo ls || atf_fail "foo file not found in listing"
302.Ed
303.Sh SEE ALSO
304.Xr atf-compile 1 ,
305.Xr atf-test-program 1 ,
306.Xr atf 7 ,
307.Xr atf-test-case 8
308