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