name
)
atf_check(
command
)
atf_check_equal(
expr1
, expr2
)
atf_config_get(
var_name
)
atf_config_has(
var_name
)
atf_fail(
reason
)
atf_get(
var_name
)
atf_get_srcdir(
)
atf_pass(
)
atf_require_prog(
prog_name
)
atf_set(
var_name
, value
)
atf_skip(
reason
)
Test programs written using this library must be preprocessed by the atf-compile(1) tool, which includes some boilerplate code and generates the final (installable) test program.
Shell-based test programs always follow this template:
atf_test_case tc1
tc1_head() {
... first test case's header ...
}
tc1_body() {
... first test case's body ...
}
atf_test_case tc2
tc2_head() {
... second test case's header ...
}
tc2_body() {
... second test case's body ...
}
tc2_cleanup() {
... second test case's cleanup ...
}
... additional test cases ...
atf_init_test_cases() {
atf_add_test_case tc1
atf_add_test_case tc2
... add additional test cases ...
}
)
function, which takes a single parameter specifiying the test case's
name and instructs the library to set things up to accept it as a valid
test case.
It is important to note that it
does not
set the test case up for execution when the program is run.
In order to do so, a later registration is needed through the
atf_add_test_case(
)
function detailed in
Program initialization.
Later on, one must define the three parts of the body by providing two
or three functions (remember that the cleanup routine is optional).
These functions are named after the test case's identifier, and are
),
)
and
)
None of these take parameters when executed.
)
function, which is in charge of registering the test cases that will be
executed at run time by using the
atf_add_test_case(
)
function, which takes the name of a test case as its single parameter.
This main function should not do anything else, except maybe sourcing
auxiliary source files that define extra variables and functions.
)
and
atf_config_get(
)
methods.
The former takes a single parameter specifying a variable name and returns
a boolean indicating whether the variable is defined or not.
The latter can take one or two parameters.
If it takes only one, it specifies the variable from which to get the
value, and this variable must be defined.
If it takes two, the second one specifies a default value to be returned
if the variable is not available.
)
function.
It is interesting to note that this can be used inside
atf_init_test_cases(
)
to silently include additional helper files from the source directory.
)
function, which takes the base name or full path of a single binary.
Relative paths are forbidden.
If it is not found, the test case will be automatically skipped.
),
atf_fail(
)
or
atf_skip(
).
These three functions terminate the execution of the test case immediately.
The cleanup routine will be processed afterwards in a completely automated
way, regardless of the test case's termination reason.
atf_pass()
does not take any parameters.
atf_fail(
)
and
atf_skip(
)
take a single string parameter that describes why the test case failed or
was skipped, respectively.
It is very important to provide a clear error message in both cases so that
the user can quickly know why the test did not pass.
cmd
, expcode
, expout
, experr
)
This function takes four parameters: the command to execute, the expected
numerical exit code, the expected behavior of
stdout
and the expected
behavior of
stderr
.
expout
can be one of the following:
expout
stdout
channel must match exactly what is
found in the
expout
file.
ignore
stdout
channel.
null
stdout
channel.
stdout
stdout
channel is written to a
stdout
file, available for further inspection.
Similarly,
experr
can be one of
`experr
',
`ignore
',
`null
',
or
`stderr
',
all of which follow the same semantics of their corresponding counterparts
for the
expout
case.
It is important to note that when a failure is detected, this function will
print as much information as possible to be able to identify the cause of
the failure.
For example, if the
stdout
does not match with the expected contents, a
diff will be printed.
atf_check_equal(expr1
, expr2
)
This function takes two expressions, evaluates them and, if their results differ, aborts the test case with an appropriate failure message.
atf_test_case addition
addition_head() {
atf_set "descr" "Sample tests for the addition operator"
}
addition_body() {
atf_check_equal $((0 + 0)) 0
atf_check_equal $((0 + 1)) 1
atf_check_equal $((1 + 0)) 0
atf_check_equal $((1 + 1)) 2
atf_check_equal $((100 + 200)) 300
}
atf_init_test_cases() {
atf_add_test_case addition
}
This other example shows how to include a file with extra helper functions in the test program:
... definition of test cases ...
atf_init_test_cases() {
. $(atf_get_srcdir)/helper_functions.sh
atf_add_test_case foo1
atf_add_test_case foo2
}
This example demonstrates the use of the very useful
atf_check()
function:
# Check for silent output
atf_check 'true' 0 null null
# Check for silent output and failure
atf_check 'false' 1 null null
# Check for known stdout and silent stderr
echo foo >expout
atf_check 'echo foo' 0 expout null
# Generate a file for later inspection
atf_check 'ls' 0 stdout null
grep foo ls || atf_fail "foo file not found in listing"