Main index | Section 3 | Options |
#include <atf-c.h>
amp;... C-specific includes go here ...#include <atf-c.h>
ATF_TC(tc1); ATF_TC_HEAD(tc1, tc) { ... first test case's header ... } ATF_TC_BODY(tc1, tc) { ... first test case's body ... }
ATF_TC_WITH_CLEANUP(tc2); ATF_TC_HEAD(tc2, tc) { ... second test case's header ... } ATF_TC_BODY(tc2, tc) { ... second test case's body ... } ATF_TC_CLEANUP(tc2, tc) { ... second test case's cleanup ... }
ATF_TC_WITHOUT_HEAD(tc3); ATF_TC_BODY(tc3, tc) { ... third test case's body ... }
amp;... additional test cases ...
ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tcs, tc1); ATF_TP_ADD_TC(tcs, tc2); ATF_TP_ADD_TC(tcs, tc3); ... add additional test cases ...
return atf_no_error(); }
Later on, one must define the three parts of the body by means of three functions. Their headers are given by the ATF_TC_HEAD(), ATF_TC_BODY() and ATF_TC_CLEANUP() macros, all of which take the test case name provided to the ATF_TC() ATF_TC_WITH_CLEANUP(), or ATF_TC_WITHOUT_HEAD() macros and the name of the variable that will hold a pointer to the test case data. Following each of these, a block of code is expected, surrounded by the opening and closing brackets.
After the macro, you are supposed to provide the body of a function, which should only use the ATF_TP_ADD_TC() macro to register the test cases the test program will execute and return a success error code. The first parameter of this macro matches the name you provided in the former call. The success status can be returned using the atf_no_error() function.
The '_wd' variants take a default value for the variable which is returned if the variable is not defined. The other functions without the '_wd' suffix require the variable to be defined.
atf_tc_pass() does not take any parameters. atf_tc_fail(), atf_tc_fail_nonfatal() and atf_tc_skip() take a format string and a variable list of parameters, which describe, in a user-friendly manner, 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_tc_expect_death(reason, ...) | |
Expects the test case to exit prematurely regardless of the nature of the exit. | |
atf_tc_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_tc_expect_fail(reason, ...) | |
Any failure (be it fatal or non-fatal) raised in this mode is recorded.
However, 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_tc_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_tc_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_tc_expect_timeout(reason, ...) | |
Expects the test case to execute for longer than its timeout. | |
The 'REQUIRE' variant of the macros immediately abort the test case as soon as an error condition is detected by calling the atf_tc_fail() function. Use this variant whenever it makes no sense to continue the execution of a test case when the checked condition is not met. The 'CHECK' variant, on the other hand, reports a failure as soon as it is encountered using the atf_tc_fail_nonfatal() function, but the execution of the test case continues as if nothing had happened. Use this variant whenever the checked condition is important as a result of the test case, but there are other conditions that can be subsequently checked on the same run without aborting.
Additionally, the 'MSG' variants take an extra set of parameters to explicitly specify the failure message. This failure message is formatted according to the printf(3) formatters.
ATF_CHECK(), ATF_CHECK_MSG(), ATF_REQUIRE() and ATF_REQUIRE_MSG() take an expression and fail if the expression evaluates to false.
ATF_CHECK_EQ(), ATF_CHECK_EQ_MSG(), ATF_REQUIRE_EQ() and ATF_REQUIRE_EQ_MSG() take two expressions and fail if the two evaluated values are not equal. The common style is to put the expected value in the first parameter and the actual value in the second parameter.
ATF_CHECK_MATCH(), ATF_CHECK_MATCH_MSG(), ATF_REQUIRE_MATCH() and ATF_REQUIRE_MATCH_MSG() take a regular expression and a string and fail if the regular expression does not match the given string. Note that the regular expression is not anchored, so it will match anywhere in the string.
ATF_CHECK_STREQ(), ATF_CHECK_STREQ_MSG(), ATF_REQUIRE_STREQ() and ATF_REQUIRE_STREQ_MSG() take two strings and fail if the two are not equal character by character. The common style is to put the expected string in the first parameter and the actual string in the second parameter.
ATF_CHECK_ERRNO() and ATF_REQUIRE_ERRNO() take, first, the error code that the check is expecting to find in the errno variable and, second, a boolean expression that, if evaluates to true, means that a call failed and errno has to be checked against the first value.
void atf_utils_cat_file(const char *file, const char *prefix) Prints the contents of file to the standard output, prefixing every line with the string in prefix.
bool atf_utils_compare_file(const char *file, const char *contents) Returns true if the given file matches exactly the expected inlined contents.
void atf_utils_copy_file(const char *source, const char *destination) Copies the file source to destination. The permissions of the file are preserved during the code.
void atf_utils_create_file(const char *file, const char *contents, ...) Creates file with the text given in contents, which is a formatting string that uses the rest of the variable arguments.
void atf_utils_file_exists(const char *file) Checks if file exists.
pid_t atf_utils_fork(void) Forks a process and redirects the standard output and standard error of the child to files for later validation with atf_utils_wait(). Fails the test case if the fork fails, so this does not return an error.
void atf_utils_free_charpp(char **argv) Frees a dynamically-allocated array of dynamically-allocated strings.
bool atf_utils_grep_file(const char *regexp, const char *file, ...) Searches for the regexp, which is a formatting string representing the regular expression, in the file. The variable arguments are used to construct the regular expression.
bool atf_utils_grep_string(const char *regexp, const char *str, ...) Searches for the regexp, which is a formatting string representing the regular expression, in the literal string str. The variable arguments are used to construct the regular expression.
char * atf_utils_readline(int fd) Reads a line from the file descriptor fd. The line, if any, is returned as a dynamically-allocated buffer that must be released with free(3). If there was nothing to read, returns 'NULL'.
void atf_utils_redirect(const int fd, const char *file) Redirects the given file descriptor fd to file. This function exits the process in case of an error and does not properly mark the test case as failed. As a result, it should only be used in subprocesses of the test case; specially those spawned by atf_utils_fork().
void atf_utils_wait(const pid_t pid, const int expected_exit_status, const char *expected_stdout, const char *expected_stderr) Waits and validates the result of a subprocess spawned with atf_utils_fork(). The validation involves checking that the subprocess exited cleanly and returned the code specified in expected_exit_status and that its standard output and standard error match the strings given in expected_stdout and expected_stderr.
If any of the expected_stdout or expected_stderr strings are prefixed with 'save:', then they specify the name of the file into which to store the stdout or stderr of the subprocess, and no comparison is performed.
ATF_BUILD_CC | |
Path to the C compiler. | |
ATF_BUILD_CFLAGS | |
C compiler flags. | |
ATF_BUILD_CPP | |
Path to the C/C++ preprocessor. | |
ATF_BUILD_CPPFLAGS | |
C/C++ preprocessor flags. | |
ATF_BUILD_CXX | |
Path to the C++ compiler. | |
ATF_BUILD_CXXFLAGS | |
C++ compiler flags. | |
#include <atf-c.h>ATF_TC(addition); ATF_TC_HEAD(addition, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the addition operator"); } ATF_TC_BODY(addition, tc) { ATF_CHECK_EQ(0, 0 + 0); ATF_CHECK_EQ(1, 0 + 1); ATF_CHECK_EQ(1, 1 + 0);
ATF_CHECK_EQ(2, 1 + 1);
ATF_CHECK_EQ(300, 100 + 200); }
ATF_TC(string_formatting); ATF_TC_HEAD(string_formatting, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the snprintf"); } ATF_TC_BODY(string_formatting, tc) { char buf[1024]; snprintf(buf, sizeof(buf), "a %s", "string"); ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working"); }
ATF_TC(open_failure); ATF_TC_HEAD(open_failure, tc) { atf_tc_set_md_var(tc, "descr", "Sample tests for the open function"); } ATF_TC_BODY(open_failure, tc) { ATF_CHECK_ERRNO(ENOENT, open("non-existent", O_RDONLY) == -1); }
ATF_TC(known_bug); ATF_TC_HEAD(known_bug, tc) { atf_tc_set_md_var(tc, "descr", "Reproduces a known bug"); } ATF_TC_BODY(known_bug, tc) { atf_tc_expect_fail("See bug number foo/bar"); ATF_CHECK_EQ(3, 1 + 1); atf_tc_expect_pass(); ATF_CHECK_EQ(3, 1 + 2); }
ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, addition); ATF_TP_ADD_TC(tp, string_formatting); ATF_TP_ADD_TC(tp, open_failure); ATF_TP_ADD_TC(tp, known_bug);
return atf_no_error(); }
ATF-C (3) | April 5, 2017 |
Main index | Section 3 | Options |
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
“ | As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. | ” |
— Maurice Wilkes |