1.\" 2.\" Automated Testing Framework (atf) 3.\" 4.\" Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.Dd July 17, 2009 30.Dt ATF-C-API 3 31.Os 32.Sh NAME 33.Nm ATF_CHECK , 34.Nm ATF_CHECK_MSG , 35.Nm ATF_CHECK_EQ , 36.Nm ATF_CHECK_EQ_MSG , 37.Nm ATF_CHECK_STREQ , 38.Nm ATF_CHECK_STREQ_MSG , 39.Nm ATF_REQUIRE , 40.Nm ATF_REQUIRE_MSG , 41.Nm ATF_REQUIRE_EQ , 42.Nm ATF_REQUIRE_EQ_MSG , 43.Nm ATF_REQUIRE_STREQ , 44.Nm ATF_REQUIRE_STREQ_MSG , 45.Nm ATF_TC , 46.Nm ATF_TC_BODY , 47.Nm ATF_TC_BODY_NAME , 48.Nm ATF_TC_CLEANUP , 49.Nm ATF_TC_CLEANUP_NAME , 50.Nm ATF_TC_HEAD , 51.Nm ATF_TC_HEAD_NAME , 52.Nm ATF_TC_NAME , 53.Nm ATF_TC_WITH_CLEANUP , 54.Nm ATF_TP_ADD_TC , 55.Nm ATF_TP_ADD_TCS , 56.Nm atf_no_error , 57.Nm atf_tc_fail , 58.Nm atf_tc_fail_nonfatal , 59.Nm atf_tc_pass , 60.Nm atf_tc_skip 61.Nd C API to write ATF-based test programs 62.Sh SYNOPSIS 63.In atf-c.h 64.Fn ATF_CHECK "expression" 65.Fn ATF_CHECK_MSG "expression" "fail_msg_fmt" ... 66.Fn ATF_CHECK_EQ "expression_1" "expression_2" 67.Fn ATF_CHECK_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ... 68.Fn ATF_CHECK_STREQ "string_1" "string_2" 69.Fn ATF_CHECK_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ... 70.Fn ATF_REQUIRE "expression" 71.Fn ATF_REQUIRE_MSG "expression" "fail_msg_fmt" ... 72.Fn ATF_REQUIRE_EQ "expression_1" "expression_2" 73.Fn ATF_REQUIRE_EQ_MSG "expression_1" "expression_2" "fail_msg_fmt" ... 74.Fn ATF_REQUIRE_STREQ "string_1" "string_2" 75.Fn ATF_REQUIRE_STREQ_MSG "string_1" "string_2" "fail_msg_fmt" ... 76.Fn ATF_TC "name" 77.Fn ATF_TC_BODY "name" "tc" 78.Fn ATF_TC_BODY_NAME "name" 79.Fn ATF_TC_CLEANUP "name" "tc" 80.Fn ATF_TC_CLEANUP_NAME "name" 81.Fn ATF_TC_HEAD "name" "tc" 82.Fn ATF_TC_HEAD_NAME "name" 83.Fn ATF_TC_NAME "name" 84.Fn ATF_TC_WITH_CLEANUP "name" 85.Fn ATF_TP_ADD_TC "tp_name" 86.Fn ATF_TP_ADD_TCS "tp_name" "tc_name" 87.Fn atf_no_error 88.Fn atf_tc_fail "reason" 89.Fn atf_no_error 90.Fn atf_tc_fail_nonfatal "reason" 91.Fn atf_tc_pass 92.Fn atf_tc_skip "reason" 93.Sh DESCRIPTION 94The ATF 95.Pp 96C-based test programs always follow this template: 97.Bd -literal -offset indent 98.Ns ... C-specific includes go here ... 99 100#include <atf-c.h> 101 102ATF_TC(tc1); 103ATF_TC_HEAD(tc1, tc) 104{ 105 ... first test case's header ... 106} 107ATF_TC_BODY(tc1, tc) 108{ 109 ... first test case's body ... 110} 111 112ATF_TC_WITH_CLEANUP(tc2); 113ATF_TC_HEAD(tc2, tc) 114{ 115 ... second test case's header ... 116} 117ATF_TC_BODY(tc2, tc) 118{ 119 ... second test case's body ... 120} 121ATF_TC_CLEANUP(tc2, tc) 122{ 123 ... second test case's cleanup ... 124} 125 126.Ns ... additional test cases ... 127 128ATF_TP_ADD_TCS(tp, tcs) 129{ 130 ATF_TP_ADD_TC(tcs, tc1) 131 ATF_TP_ADD_TC(tcs, tc2) 132 ... add additional test cases ... 133 134 return atf_no_error(); 135} 136.Ed 137.Ss Definition of test cases 138Test cases have an identifier and are composed of three different parts: 139the header, the body and an optional cleanup routine, all of which are 140described in 141.Xr atf-test-case 8 . 142To define test cases, one can use the 143.Fn ATF_TC 144or the 145.Fn ATF_TC_WITH_CLEANUP 146macros, which take a single parameter specifiying the test case's name. 147The former does not allow the specification of a cleanup routine for the 148test case while the latter does. 149It is important to note that these 150.Em do not 151set the test case up for execution when the program is run. 152In order to do so, a later registration is needed with the 153.Fn ATF_TP_ADD_TC 154macro detailed in 155.Sx Program initialization . 156.Pp 157Later on, one must define the three parts of the body by means of three 158functions. 159Their headers are given by the 160.Fn ATF_TC_HEAD , 161.Fn ATF_TC_BODY 162and 163.Fn ATF_TC_CLEANUP 164macros, all of which take the test case name provided to the 165.Fn ATF_TC 166or 167.Fn ATF_TC_WITH_CLEANUP 168macros and the name of the variable that will hold a pointer to the 169test case data. 170Following each of these, a block of code is expected, surrounded by the 171opening and closing brackets. 172.Ss Program initialization 173The library provides a way to easily define the test program's 174.Fn main 175function. 176You should never define one on your own, but rely on the 177library to do it for you. 178This is done by using the 179.Fn ATF_TP_ADD_TCS 180macro, which is passed the name of the object that will hold the test 181cases; i.e. the test program instance. 182This name can be whatever you want as long as it is a valid variable 183identifier. 184.Pp 185After the macro, you are supposed to provide the body of a function, which 186should only use the 187.Fn ATF_TP_ADD_TC 188macro to register the test cases the test program will execute and return 189a success error code. 190The first parameter of this macro matches the name you provided in the 191former call. 192The success status can be returned using the 193.Fn atf_no_error 194function. 195.Ss Header definitions 196The test case's header can define the meta-data by using the 197.Fn atf_tc_set_md_var 198method, which takes three parameters: the first one points to the test 199case data, the second one specifies the meta-data variable to be set 200and the third one specifies its value. 201Both of them are strings. 202.Ss Configuration variables 203The test case has read-only access to the current configuration variables 204by means of the 205.Ft bool 206.Fn atf_tc_has_config_var 207and the 208.Ft const char * 209.Fn atf_tc_get_config_var 210methods, which can be called in any of the three parts of a test case. 211.Ss Access to the source directory 212It is possible to get the path to the test case's source directory from any 213of its three components by querying the 214.Sq srcdir 215configuration variable. 216.Ss Requiring programs 217Aside from the 218.Va require.progs 219meta-data variable available in the header only, one can also check for 220additional programs in the test case's body by using the 221.Fn atf_tc_require_prog 222function, which takes the base name or full path of a single binary. 223Relative paths are forbidden. 224If it is not found, the test case will be automatically skipped. 225.Ss Test case finalization 226The test case finalizes either when the body reaches its end, at which 227point the test is assumed to have 228.Em passed , 229unless any non-fatal errors were raised using 230.Fn atf_tc_fail_nonfatal , 231or at any explicit call to 232.Fn atf_tc_pass , 233.Fn atf_tc_fail 234or 235.Fn atf_tc_skip . 236These three functions terminate the execution of the test case immediately. 237The cleanup routine will be processed afterwards in a completely automated 238way, regardless of the test case's termination reason. 239.Pp 240.Fn atf_tc_pass 241does not take any parameters. 242.Fn atf_tc_fail , 243.Fn atf_tc_fail_nonfatal 244and 245.Fn atf_tc_skip 246take a format string and a variable list of parameters, which describe, in 247a user-friendly manner, why the test case failed or was skipped, 248respectively. 249It is very important to provide a clear error message in both cases so that 250the user can quickly know why the test did not pass. 251.Ss Helper macros for common checks 252The library provides several macros that are very handy in multiple 253situations. 254These basically check some condition after executing a given statement or 255processing a given expression and, if the condition is not met, they 256report the test case as failed. 257.Pp 258The 259.Sq REQUIRE 260variant of the macros immediately abort the test case as soon as an error 261condition is detected by calling the 262.Fn atf_tc_fail 263function. 264Use this variant whenever it makes now sense to continue the execution of a 265test case when the checked condition is not met. 266The 267.Sq CHECK 268variant, on the other hand, reports a failure as soon as it is encountered 269using the 270.Fn atf_tc_fail_nonfatal 271function, but the execution of the test case continues as if nothing had 272happened. 273Use this variant whenever the checked condition is important as a result of 274the test case, but there are other conditions that can be subsequently 275checked on the same run without aborting. 276.Pp 277Additionally, the 278.Sq MSG 279variants take an extra set of parameters to explicitly specify the failure 280message. 281This failure message is formatted according to the 282.Xr printf 3 283formatters. 284.Pp 285.Fn ATF_CHECK , 286.Fn ATF_CHECK_MSG , 287.Fn ATF_REQUIRE 288and 289.Fn ATF_REQUIRE_MSG 290take an expression and fail if the expression evaluates to false. 291.Pp 292.Fn ATF_CHECK_EQ , 293.Fn ATF_CHECK_EQ_MSG , 294.Fn ATF_REQUIRE_EQ 295and 296.Fn ATF_REQUIRE_EQ_MSG 297take two expressions and fail if the two evaluated values are not equal. 298.Pp 299.Fn ATF_CHECK_STREQ , 300.Fn ATF_CHECK_STREQ_MSG , 301.Fn ATF_REQUIRE_STREQ 302and 303.Fn ATF_REQUIRE_STREQ_MSG 304take two strings and fail if the two are not equal character by character. 305.Sh EXAMPLES 306The following shows a complete test program with a single test case that 307validates the addition operator: 308.Bd -literal -offset indent 309#include <atf-c.h> 310 311ATF_TC(addition); 312ATF_TC_HEAD(addition, tc) 313{ 314 atf_tc_set_md_var(tc, "descr", 315 "Sample tests for the addition operator"); 316} 317ATF_TC_BODY(addition, tc) 318{ 319 ATF_CHECK_EQ(0 + 0, 0); 320 ATF_CHECK_EQ(0 + 1, 1); 321 ATF_CHECK_EQ(1 + 0, 1); 322 323 ATF_CHECK_EQ(1 + 1, 2); 324 325 ATF_CHECK_EQ(100 + 200, 300); 326} 327 328ATF_TC(string_formatting); 329ATF_TC_HEAD(string_formatting, tc) 330{ 331 atf_tc_set_md_var(tc, "descr", 332 "Sample tests for the snprintf"); 333} 334ATF_TC_BODY(string_formatting, tc) 335{ 336 char buf[1024]; 337 snprintf(buf, sizeof(buf), "a %s", "string"); 338 ATF_CHECK_STREQ_MSG("a string", buf, "%s is not working"); 339} 340 341ATF_TP_ADD_TCS(tp) 342{ 343 ATF_TP_ADD_TC(tp, addition); 344 ATF_TP_ADD_TC(tp, string_formatting); 345 346 return atf_no_error(); 347} 348.Ed 349.Sh SEE ALSO 350.Xr atf-test-program 1 , 351.Xr atf 7 , 352.Xr atf-test-case 8 353