Main index | Section 3 | Options |
Test programs written using this library must be run using the atf-sh(1) interpreter by putting the following on their very first line:
#! /usr/bin/env atf-sh
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 cleanup tc2_head() { ... second test case's header ... } tc2_body() { ... second test case's body ... } tc2_cleanup() { ... second test case's cleanup ... }
amp;... additional test cases ...
atf_init_test_cases() { atf_add_test_case tc1 atf_add_test_case tc2 ... add additional test cases ... }
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 <id>_head, <id>_body and <id>_cleanup. None of these take parameters when executed.
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.
Each test case has an internal state called 'expect' that describes what the test case expectations are at any point in time. The value of this property can change during execution by any of:
atf_expect_death | |
Expects the test case to exit prematurely regardless of the nature of the exit. | |
atf_expect_exit | |
Expects the test case to exit cleanly. If exitcode is not '-1', the runtime engine will validate that the exit code of the test case matches the one provided in this call. Otherwise, the exact value will be ignored. | |
atf_expect_fail | |
Any failure raised in this mode is recorded, but such failures do not report
the test case as failed; instead, the test case finalizes cleanly and is
reported as
'expected failure';
this report includes the provided
reason
as part of it.
If no error is raised while running in this mode, then the test case is
reported as
'failed'.
This mode is useful to reproduce actual known bugs in tests. Whenever the developer fixes the bug later on, the test case will start reporting a failure, signaling the developer that the test case must be adjusted to the new conditions. In this situation, it is useful, for example, to set reason as the bug number for tracking purposes. | |
atf_expect_pass | |
This is the normal mode of execution. In this mode, any failure is reported as such to the user and the test case is marked as 'failed'. | |
atf_expect_signal | |
Expects the test case to terminate due to the reception of a signal. If signo is not '-1', the runtime engine will validate that the signal that terminated the test case matches the one provided in this call. Otherwise, the exact value will be ignored. | |
atf_expect_timeout | |
Expects the test case to execute for longer than its timeout. | |
atf_check | |
Executes a command, performs checks on its exit code and its output, and
fails the test case if any of the checks is not successful.
This function is particularly useful in integration tests that verify the
correct functioning of a binary.
Internally, this function is just a wrapper over the atf-check(1) tool (whose manual page provides all details on the calling syntax). You should always use the atf_check function instead of the atf-check(1) tool in your scripts; the latter is not even in the path. | |
atf_check_equal | |
This function takes two expressions, evaluates them and, if their results differ, aborts the test case with an appropriate failure message. The common style is to put the expected value in the first parameter and the actual value in the second parameter. | |
atf_check_not_equal | |
This function takes two expressions, evaluates them and, if their results are equal, aborts the test case with an appropriate failure message. The common style is to put the expected value in the first parameter and the actual value in the second parameter. | |
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 1 $((0 + 1)) atf_check_equal 1 $((1 + 0))atf_check_equal 2 $((1 + 1))
atf_check_equal 300 $((100 + 200)) }
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:
amp;... 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 -s exit:0 -o empty -e empty 'true'# Check for silent output and failure atf_check -s exit:1 -o empty -e empty 'false'
# Check for known stdout and silent stderr echo foo >expout atf_check -s exit:0 -o file:expout -e empty 'echo foo'
# Generate a file for later inspection atf_check -s exit:0 -o save:stdout -e empty 'ls' grep foo ls || atf_fail "foo file not found in listing"
# Or just do the match along the way atf_check -s exit:0 -o match:"^foo$" -e empty 'ls'
ATF-SH (3) | June 08, 2017 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | The Unix phenomenon is scary. It doesn't go away. | ” |
— Steve Ballmer |