1ATF-SH-API(3)            BSD Library Functions Manual            ATF-SH-API(3)
2

NAME

4     atf_add_test_case, atf_check, atf_check_equal, atf_config_get,
5     atf_config_has, atf_expect_death, atf_expect_exit, atf_expect_fail,
6     atf_expect_pass, atf_expect_signal, atf_expect_timeout, atf_fail,
7     atf_get, atf_get_srcdir, atf_pass, atf_require_prog, atf_set, atf_skip,
8     atf_test_case — POSIX shell API to write ATF-based test programs
9

SYNOPSIS

11     atf_add_test_case(name);
12
13     atf_check(command);
14
15     atf_check_equal(expr1, expr2);
16
17     atf_config_get(var_name);
18
19     atf_config_has(var_name);
20
21     atf_expect_death(reason, ...);
22
23     atf_expect_exit(exitcode, reason, ...);
24
25     atf_expect_fail(reason, ...);
26
27     atf_expect_pass();
28
29     atf_expect_signal(signo, reason, ...);
30
31     atf_expect_timeout(reason, ...);
32
33     atf_fail(reason);
34
35     atf_get(var_name);
36
37     atf_get_srcdir();
38
39     atf_pass();
40
41     atf_require_prog(prog_name);
42
43     atf_set(var_name, value);
44
45     atf_skip(reason);
46
47     atf_test_case(name, cleanup);
48

DESCRIPTION

50     ATF provides a simple but powerful interface to easily write test pro‐
51     grams in the POSIX shell language.  These are extremely helpful given
52     that they are trivial to write due to the language simplicity and the
53     great deal of available external tools, so they are often ideal to test
54     other applications at the user level.
55
56     Test programs written using this library must be run using the atf-sh(1)
57     interpreter by putting the following on their very first line:
58
59           #! /usr/bin/env atf-sh
60
61     Shell-based test programs always follow this template:
62
63           atf_test_case tc1
64           tc1_head() {
65               ... first test case's header ...
66           }
67           tc1_body() {
68               ... first test case's body ...
69           }
70
71           atf_test_case tc2 cleanup
72           tc2_head() {
73               ... second test case's header ...
74           }
75           tc2_body() {
76               ... second test case's body ...
77           }
78           tc2_cleanup() {
79               ... second test case's cleanup ...
80           }
81
82           ... additional test cases ...
83
84           atf_init_test_cases() {
85               atf_add_test_case tc1
86               atf_add_test_case tc2
87               ... add additional test cases ...
88           }
89
90   Definition of test cases
91     Test cases have an identifier and are composed of three different parts:
92     the header, the body and an optional cleanup routine, all of which are
93     described in atf-test-case(4).  To define test cases, one can use the
94     atf_test_case() function, which takes a first parameter specifiying the
95     test case's name and instructs the library to set things up to accept it
96     as a valid test case.  The second parameter is optional and, if provided,
97     must be ‘cleanup’; providing this parameter allows defining a cleanup
98     routine for the test case.  It is important to note that this function
99     does not set the test case up for execution when the program is run.  In
100     order to do so, a later registration is needed through the
101     atf_add_test_case() function detailed in Program initialization.
102
103     Later on, one must define the three parts of the body by providing two or
104     three functions (remember that the cleanup routine is optional).  These
105     functions are named after the test case's identifier, and are
106     <id>_head(), <id>_body() and <id>_cleanup.() None of these take parame‐
107     ters when executed.
108
109   Program initialization
110     The test program must define an atf_init_test_cases() function, which is
111     in charge of registering the test cases that will be executed at run time
112     by using the atf_add_test_case() function, which takes the name of a test
113     case as its single parameter.  This main function should not do anything
114     else, except maybe sourcing auxiliary source files that define extra
115     variables and functions.
116
117   Configuration variables
118     The test case has read-only access to the current configuration variables
119     through the atf_config_has() and atf_config_get() methods.  The former
120     takes a single parameter specifying a variable name and returns a boolean
121     indicating whether the variable is defined or not.  The latter can take
122     one or two parameters.  If it takes only one, it specifies the variable
123     from which to get the value, and this variable must be defined.  If it
124     takes two, the second one specifies a default value to be returned if the
125     variable is not available.
126
127   Access to the source directory
128     It is possible to get the path to the test case's source directory from
129     anywhere in the test program by using the atf_get_srcdir() function.  It
130     is interesting to note that this can be used inside atf_init_test_cases()
131     to silently include additional helper files from the source directory.
132
133   Requiring programs
134     Aside from the require.progs meta-data variable available in the header
135     only, one can also check for additional programs in the test case's body
136     by using the atf_require_prog() function, which takes the base name or
137     full path of a single binary.  Relative paths are forbidden.  If it is
138     not found, the test case will be automatically skipped.
139
140   Test case finalization
141     The test case finalizes either when the body reaches its end, at which
142     point the test is assumed to have passed, or at any explicit call to
143     atf_pass(), atf_fail() or atf_skip().  These three functions terminate
144     the execution of the test case immediately.  The cleanup routine will be
145     processed afterwards in a completely automated way, regardless of the
146     test case's termination reason.
147
148     atf_pass() does not take any parameters.  atf_fail() and atf_skip() take
149     a single string parameter that describes why the test case failed or was
150     skipped, respectively.  It is very important to provide a clear error
151     message in both cases so that the user can quickly know why the test did
152     not pass.
153
154   Expectations
155     Everything explained in the previous section changes when the test case
156     expectations are redefined by the programmer.
157
158     Each test case has an internal state called ‘expect’ that describes what
159     the test case expectations are at any point in time.  The value of this
160     property can change during execution by any of:
161
162     atf_expect_death(reason, ...)
163             Expects the test case to exit prematurely regardless of the
164             nature of the exit.
165
166     atf_expect_exit(exitcode, reason, ...)
167             Expects the test case to exit cleanly.  If exitcode is not ‘-1’,
168             atf-run(1) will validate that the exit code of the test case
169             matches the one provided in this call.  Otherwise, the exact
170             value will be ignored.
171
172     atf_expect_fail(reason)
173             Any failure raised in this mode is recorded, but such failures do
174             not report the test case as failed; instead, the test case final‐
175             izes cleanly and is reported as ‘expected failure’; this report
176             includes the provided reason as part of it.  If no error is
177             raised while running in this mode, then the test case is reported
178             as ‘failed’.
179
180             This mode is useful to reproduce actual known bugs in tests.
181             Whenever the developer fixes the bug later on, the test case will
182             start reporting a failure, signaling the developer that the test
183             case must be adjusted to the new conditions.  In this situation,
184             it is useful, for example, to set reason as the bug number for
185             tracking purposes.
186
187     atf_expect_pass()
188             This is the normal mode of execution.  In this mode, any failure
189             is reported as such to the user and the test case is marked as
190             ‘failed’.
191
192     atf_expect_signal(signo, reason, ...)
193             Expects the test case to terminate due to the reception of a sig‐
194             nal.  If signo is not ‘-1’, atf-run(1) will validate that the
195             signal that terminated the test case matches the one provided in
196             this call.  Otherwise, the exact value will be ignored.
197
198     atf_expect_timeout(reason, ...)
199             Expects the test case to execute for longer than its timeout.
200
201   Helper functions for common checks
202     atf_check([options], command, [args])
203
204     This function wraps the execution of the atf-check tool and makes the
205     test case fail if the tool reports failure.  You should always use this
206     function instead of the tool in your scripts.  For more details on the
207     parameters of this function, refer to atf-check(1).
208
209     atf_check_equal(expr1, expr2)
210
211     This function takes two expressions, evaluates them and, if their results
212     differ, aborts the test case with an appropriate failure message.
213

EXAMPLES

215     The following shows a complete test program with a single test case that
216     validates the addition operator:
217
218           atf_test_case addition
219           addition_head() {
220               atf_set "descr" "Sample tests for the addition operator"
221           }
222           addition_body() {
223               atf_check_equal $((0 + 0)) 0
224               atf_check_equal $((0 + 1)) 1
225               atf_check_equal $((1 + 0)) 0
226
227               atf_check_equal $((1 + 1)) 2
228
229               atf_check_equal $((100 + 200)) 300
230           }
231
232           atf_init_test_cases() {
233               atf_add_test_case addition
234           }
235
236     This other example shows how to include a file with extra helper func‐
237     tions in the test program:
238
239           ... definition of test cases ...
240
241           atf_init_test_cases() {
242               . $(atf_get_srcdir)/helper_functions.sh
243
244               atf_add_test_case foo1
245               atf_add_test_case foo2
246           }
247
248     This example demonstrates the use of the very useful atf_check() func‐
249     tion:
250
251           # Check for silent output
252           atf_check -s exit:0 -o empty -e empty 'true'
253
254           # Check for silent output and failure
255           atf_check -s exit:1 -o empty -e empty 'false'
256
257           # Check for known stdout and silent stderr
258           echo foo >expout
259           atf_check -s exit:0 -o file:expout -e empty 'echo foo'
260
261           # Generate a file for later inspection
262           atf_check -s exit:0 -o save:stdout -e empty 'ls'
263           grep foo ls || atf_fail "foo file not found in listing"
264
265           # Or just do the match along the way
266           atf_check -s exit:0 -o match:"^foo$" -e empty 'ls'
267

SEE ALSO

269     atf-sh(1), atf-test-program(1), atf-test-case(4), atf(7)
270
271BSD                            October 13, 2013                            BSD
Impressum