ATF-SH(3)

๐Ÿ“ฆ
version 0.22 ยท 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

๐ŸŽ€ ATF-SH(3) ๐ŸŽ€ โ€” bestie, it's the POSIX shell ATF API

okay so this is atf-sh(3), the library functions manual. It's the POSIX shell API to write ATF-based test programs. If you're writing shell tests and want them to hit different, this is your moment. ๐Ÿ’…

๐ŸŒˆ NAME ๐ŸŒˆ

Meet the cast: atf_add_test_case, atf_check, atf_check_equal, atf_check_not_equal, atf_config_get, atf_config_has, atf_expect_death, atf_expect_exit, atf_expect_fail, atf_expect_pass, atf_expect_signal, atf_expect_timeout, atf_fail, atf_get, atf_get_srcdir, atf_pass, atf_require_prog, atf_set, atf_skip, atf_test_case โ€” they're all part of the POSIX shell API to write ATF-based test programs. Periodt.

โœจ SYNOPSIS โœจ

atf_add_test_case "name"
atf_check "command"
atf_check_equal "expected_expression" "actual_expression"
atf_check_not_equal "expected_expression" "actual_expression"
atf_config_get "var_name"
atf_config_has "var_name"
atf_expect_death "reason" "..."
atf_expect_exit "exitcode" "reason" "..."
atf_expect_fail "reason" "..."
atf_expect_pass ""
atf_expect_signal "signo" "reason" "..."
atf_expect_timeout "reason" "..."
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"
atf_test_case "name" "cleanup"

๐Ÿ’– DESCRIPTION ๐Ÿ’–

ATF is like the ultimate glow-up for writing test programs in the POSIX shell language โ€” simple but powerful, no cap. Why are shell tests so iconic? Because the language is trivial to write, and there are a ton of external tools available, so they're often ideal for testing other applications at the user level. It's giving "practical and cute." โœจ

Test programs written using this library must be run using the atf-sh(1) interpreter. How? Put this 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 ...
}

... additional test cases ...

atf_init_test_cases() {
    atf_add_test_case tc1
    atf_add_test_case tc2
    ... add additional test cases ...
}

๐ŸŒธ Definition of test cases ๐ŸŒธ

Test cases have an identifier, bestie, and they're made of three parts: the header, the body, and an optional cleanup routine. All of that is described in atf-test-case(4). To define test cases, you use the atf_test_case function. First parameter? The test case's name. It tells the library to set things up so that name is accepted as a valid test case. The second parameter is optional, and if you provide it, it must be cleanup; that's how you define a cleanup routine for the test case. Important: this function does not set the test case up for execution when the program runs. To actually do that, you need a later registration through the atf_add_test_case function, which is detailed in โ€œProgram initializationโ€. Don't skip that part, okay?

Later on, you gotta define the three parts of the body by providing two or three functions โ€” remember, cleanup is optional. These functions are named after the test case's identifier: <id>_head, <id>_body, and <id>_cleanup. None of them take parameters when executed.

๐Ÿงš Program initialization ๐Ÿงš

Your test program must define an atf_init_test_cases function. Its whole job is registering the test cases that will be executed at run time, using the atf_add_test_case function. That function 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. That's it. Stay focused, queen.

๐ŸŒธ Configuration variables ๐ŸŒธ

The test case has read-only access to the current configuration variables through atf_config_has and atf_config_get. The former takes one parameter โ€” a variable name โ€” and returns a boolean telling you whether the variable is defined or not. The latter can take one or two parameters. If it takes only one, that's the variable you want the value from, and that variable must be defined. If it takes two, the second one is the default value to return if the variable is not available. Cute, right?

๐Ÿซถ Access to the source directory ๐Ÿซถ

You can get the path to the test case's source directory from anywhere in the test program with the atf_get_srcdir function. And here's a fun fact: you can use it inside atf_init_test_cases to silently include additional helper files from the source directory. Sneaky but valid. ๐Ÿ’…

๐Ÿ‘‘ Requiring programs ๐Ÿ‘‘

Besides the require.progs meta-data variable that's available in the header only, you can also check for extra programs in the test case's body using atf_require_prog. It takes the base name or full path of a single binary. Relative paths are forbidden โ€” do not try it. If the program is not found, the test case will be automatically skipped. No drama, it just skips.

๐Ÿ’… Test case finalization ๐Ÿ’…

A test case finalizes either when the body reaches its end โ€” in which case the test is assumed to have passed โ€” or at any explicit call to atf_pass, atf_fail, or atf_skip. These three functions terminate the execution of the test case immediately. After that, the cleanup routine will be processed in a completely automated way, regardless of the test case's termination reason. So cleanup always gets its moment.

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. It is very important to provide a clear error message in both cases so the user can quickly know why the test did not pass. Be kind, give context. ๐Ÿ˜ญ

๐Ÿ”ฎ Expectations ๐Ÿ”ฎ

Everything explained in the previous section changes when the test case expectations are redefined by the programmer. Yes, the plot twists.

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 these:

  • atf_expect_death "reason" "..." โ€” Expects the test case to exit prematurely regardless of the nature of the exit.
  • atf_expect_exit "exitcode" "reason" "..." โ€” 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 "reason" โ€” 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 "signo" "reason" "..." โ€” 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 "reason" "..." โ€” Expects the test case to execute for longer than its timeout.

๐Ÿ’– Helper functions for common checks ๐Ÿ’–

  • atf_check "[options]" "command" "[args]" โ€” 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 "expected_expression" "actual_expression" โ€” 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 "expected_expression" "actual_expression" โ€” 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.

๐Ÿ’– EXAMPLES ๐Ÿ’–

okay bestie this is the part where it all clicks, fr fr. here's a whole entire test program with ONE test case that validates the addition operator:

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
}

see how cute that is?? atf_test_case addition declares it, addition_head() sets the metadata, addition_body() is where the actual checking happens, and atf_init_test_cases() registers it with atf_add_test_case. the whole lifecycle in like twelve lines ๐Ÿ’…


๐Ÿฆ‹ sourcing extra helpers ๐Ÿฆ‹

this next one shows how to pull in a file with extra helper functions inside your 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
}

atf_get_srcdir gets you the source directory of the test, and then you just . (source) your helper script from there. reusable helper functions?? we love to see it ๐Ÿซถ


โœจ the atf_check glow-up โœจ

and now, the main event. 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 stdout || 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

let's break the vibes down, one by one:

  • atf_check -s exit:0 -o empty -e empty true โ€” checking for silent output. true exits 0, prints nothing, errors nothing. all three conditions satisfied, slay.
  • atf_check -s exit:1 -o empty -e empty false โ€” checking for silent output AND failure. false exits 1, still prints nothing. iconic.
  • echo foo >expout then atf_check -s exit:0 -o file:expout -e empty echo foo โ€” you write the expected output into a file called expout, then check for known stdout with -o file:expout and silent stderr. ๐Ÿ’–
  • atf_check -s exit:0 -o save:stdout -e empty ls then grep foo stdout || atf_fail "foo file not found in listing" โ€” generate a file for later inspection. -o save:stdout stashes the output into stdout, and then you can grep it yourself and call atf_fail with your own message if it's not there. DIY detective work ๐Ÿ”
  • atf_check -s exit:0 -o match:"^foo$" -e empty ls โ€” or just do the match along the way with -o match:"^foo$". no temp files, no extra steps, just vibes โœจ
ngl the save: + grep combo and the match: one-liner do kinda the same job โ€” the first one is for when you wanna look at the output with your own eyes later, the second is when you already know exactly what you're hunting for. pick your fighter ๐Ÿ’€

๐ŸŒธ SEE ALSO ๐ŸŒธ

go read those too bestie, they're the supporting cast and they ate ๐Ÿ‘‘


and that's a wrap on the whole page!! this one came from the Debian crew, dated January 27, 2021, and it's the ATF-SH(3) manual page โ€” that's the source and version and date all in one cute little sign-off, because you deserve to know where your lore comes from ๐ŸŽ€

now go write some tests, pass some checks, and remember: if your test fails, that's not a you problem, that's a code problem, and you found it ๐Ÿ’œโœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from