ATF-C-API(3) | Library Functions Manual | ATF-C-API(3) |
ATF_CHECK(expression);
ATF_CHECK_MSG(expression, fail_msg_fmt, ...);
ATF_CHECK_EQ(expression_1, expression_2);
ATF_CHECK_EQ_MSG(expression_1, expression_2, fail_msg_fmt, ...);
ATF_CHECK_STREQ(string_1, string_2);
ATF_CHECK_STREQ_MSG(string_1, string_2, fail_msg_fmt, ...);
ATF_CHECK_ERRNO(exp_errno, bool_expression);
ATF_REQUIRE(expression);
ATF_REQUIRE_MSG(expression, fail_msg_fmt, ...);
ATF_REQUIRE_EQ(expression_1, expression_2);
ATF_REQUIRE_EQ_MSG(expression_1, expression_2, fail_msg_fmt, ...);
ATF_REQUIRE_STREQ(string_1, string_2);
ATF_REQUIRE_STREQ_MSG(string_1, string_2, fail_msg_fmt, ...);
ATF_REQUIRE_ERRNO(exp_errno, bool_expression);
ATF_TC(name);
ATF_TC_BODY(name, tc);
ATF_TC_BODY_NAME(name);
ATF_TC_CLEANUP(name, tc);
ATF_TC_CLEANUP_NAME(name);
ATF_TC_HEAD(name, tc);
ATF_TC_HEAD_NAME(name);
ATF_TC_NAME(name);
ATF_TC_WITH_CLEANUP(name);
ATF_TC_WITHOUT_HEAD(name);
ATF_TP_ADD_TC(tp_name, tc_name);
ATF_TP_ADD_TCS(tp_name);
atf_tc_get_config_var(tc, varname);
atf_tc_get_config_var_wd(tc, variable_name, default_value);
atf_tc_get_config_var_as_bool(tc, variable_name);
atf_tc_get_config_var_as_bool_wd(tc, variable_name, default_value);
atf_tc_get_config_var_as_long(tc, variable_name);
atf_tc_get_config_var_as_long_wd(tc, variable_name, default_value);
atf_no_error();
atf_tc_expect_death(reason, ...);
atf_tc_expect_exit(exitcode, reason, ...);
atf_tc_expect_fail(reason, ...);
atf_tc_expect_pass();
atf_tc_expect_signal(signo, reason, ...);
atf_tc_expect_timeout(reason, ...);
atf_tc_fail(reason);
atf_tc_fail_nonfatal(reason);
atf_tc_pass();
atf_tc_skip(reason);
C-based test programs always follow this template:
... 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 ... } ... 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:
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.
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 now 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.
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.
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.
#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(0 + 1, 1); ATF_CHECK_EQ(1 + 0, 1); ATF_CHECK_EQ(1 + 1, 2); ATF_CHECK_EQ(100 + 200, 300); } 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(); }
December 26, 2010 | NetBSD 6.1 |