Annotation of wikisrc/tutorials/atf.mdwn, revision 1.9

1.5       jmmv        1: [[!meta title="Creating atf-based tests for NetBSD src"]]
                      2: [[!toc ]]
                      3: 
1.7       jmmv        4: This quick tutorial provides a guideline on how to start creating new test
1.1       jmmv        5: programs and/or test cases, how these tests are tied to the NetBSD source tree
1.7       jmmv        6: and includes a short reference of the most commonly used functions.
1.1       jmmv        7: 
                      8: You should start by reading the
                      9: [tests(7)](http://netbsd.gw.com/cgi-bin/man-cgi?tests++NetBSD-current) manual
1.7       jmmv       10: page, which provides a user-level overview on how to run the tests included in
                     11: NetBSD.  While reading this tutorial, you may also want to refer to these pages
                     12: on a need-to-know basis:
1.1       jmmv       13: [atf-run(1)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-run++NetBSD-current),
                     14: [atf-report(1)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-report++NetBSD-current),
                     15: [atf-test-program(1)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-test-program++NetBSD-current),
1.7       jmmv       16: [atf-c-api(3)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-c-api++NetBSD-current),
                     17: [atf-sh-api(3)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-sh-api++NetBSD-current)
1.1       jmmv       18: and
1.7       jmmv       19: [atf-check(1)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-check++NetBSD-current).
1.1       jmmv       20: 
                     21: **IMPORTANT: Do not take anything for granted, SPECIALLY if you have previously
                     22: worked with and/or have seen src/regress/.  Your assumptions are most likely
1.2       jmmv       23: incorrect.**
1.1       jmmv       24: 
1.5       jmmv       25: # Test programs vs. test cases
1.1       jmmv       26: 
                     27: So, what is what and how do you organize your tests?
                     28: 
                     29: A **test case** is a piece of code that exercises a particular functionality of
                     30: another piece of code.  Commonly, test cases validate the outcome of a
                     31: particular source function or class method, the validity of the execution of a
                     32: command with a particular combination of flags/arguments, etc.  Test cases are
                     33: supposed to be very concise, in the sense that they should just be testing *one
                     34: behavior*.
                     35: 
                     36: A **test program** is a binary that collects and exposes a group of test cases.
                     37: Typically, these test programs expose conceptually-related tests or all the
                     38: tests for a particular source file.
                     39: 
                     40: In general, having many test programs with **just one test case** in them is
                     41: **wrong** and smells from the previous layout of src/regress/.  Think about some
                     42: other organization.  And don't blame atf for this separation: this is extremely
                     43: common in (almost?) all other test frameworks and, when used wisely, becomes an
                     44: invaluable classification.
                     45: 
                     46: For example, suppose you have the following fictitious source files for the ls
                     47: tool:
                     48: 
                     49: * bin/ls/fs.c: Provides the list_files() and stat_files() functions.
                     50: 
                     51: * bin/ls/ui.c: Provides the format_columns() function.
                     52: 
                     53: * bin/ls/main.c: The main method for ls.
                     54: 
                     55: Then, you could define the following test programs and test cases:
                     56: 
                     57: * bin/ls/fs_test.c: Provides test cases for list_files and stat_files.  These
                     58:   would be named list_files__empty_directory, list_files__one_file,
                     59:   list_files__multiple_files, stat_files__directory, stat_files__symlink, etc.
                     60: 
                     61: * bin/ls/ui_test.c: Provides test cases for the format_columns function.  These
                     62:   would be named format_columns__no_files, format_columns__multiple_files, etc.
                     63: 
                     64: * bin/ls/integration_test.sh: Provides "black box" test cases for the binary
                     65:   itself.  These would be named lflag, lflag_and_Fflag, no_flags, no_files, etc.
                     66: 
1.2       jmmv       67: Try to keep your test case names as descriptive as possible so that they do not
                     68: require comments to explain what they intend to test.
                     69: 
1.5       jmmv       70: # Test case parts
1.3       jmmv       71: 
1.5       jmmv       72: ## The head
1.3       jmmv       73: 
                     74: The *head* is used **for the sole purpose** to define meta-data properties for
                     75: the test case.  (Eventually, this would not be specified programmatically, but
                     76: is how we deal with the information right now.)
                     77: 
                     78: The following properties are commonly useful:
                     79: 
                     80: * descr: A textual description of the purpose of the test case.
                     81: 
1.7       jmmv       82: * require.user: Set to 'root' to tell the atf runtime that this test requires
                     83:   root privileges.  The test will later be skipped if you are running atf as
                     84:   non-root, and the test will be executed otherwise.
1.3       jmmv       85: 
                     86: * use.fs: Set to 'true' if the test case creates temporary files in the "current
1.7       jmmv       87:   directory".  If set to false, the atf runtime will set the "current directory"
                     88:   to an unwritable directory, which will disallow the creation of the temporary
                     89:   files and will make your test misteriously fail.
1.3       jmmv       90: 
1.5       jmmv       91: ## The body
1.3       jmmv       92: 
                     93: The *body* is the actual meat of the test case.  This is just a regular function
                     94: that executes any code you want and calls special atf functions to report
                     95: failures; see below.
                     96: 
                     97: In particular, be aware that the atf run-time **isolates** the execution of
                     98: every test case to prevent side-effects (such as temporary file leftovers,
                     99: in-memory data corruption, etc.).  In particular:
                    100: 
                    101: * A test case is **always executed as a subprocess** that is separate from the
1.9     ! jmmv      102:   head.  This implies that you cannot pass any in-memory state between the
        !           103:   parts.
1.3       jmmv      104: 
                    105: * The current working directory of a test case is changed to a temporary
                    106:   location that gets cleaned up later on automatically.  (Set the use.fs
                    107:   property to true in the head if you need to write to this temporary
                    108:   directory.)
                    109: 
                    110: * The environment of the test case is "sanitized" to get rid of variables that
                    111:   can cause side-effects; e.g. LC_ALL, TZ, etc.
                    112: 
1.7       jmmv      113: # Running the test programs
                    114: 
                    115: Do:
                    116: 
                    117:     $ cd /usr/tests/
                    118:     $ atf-run | atf-report
                    119: 
                    120: Why?
1.2       jmmv      121: 
                    122: Test programs get installed into the /usr/tests/ hierarchy.  The main reason for
                    123: doing that is to allow *any* user to test his system and to be able to convince
                    124: himself that everything is working correctly.
                    125: 
                    126: Imagine that you install NetBSD-current on a public-facing machine that has some
                    127: particular hardware only supported in the bleeding-edge source tree.  In this
                    128: scenario, you, as the administrator, could just go into /usr/tests/, run the
                    129: tests and know immediately if everything is working correctly in your
                    130: software+hardware combination or not.  No need to rely on promises from the
                    131: vendor, no need to deal with a source tree, no need to have a compiler
                    132: installed...
                    133: 
                    134: So, that's the theory.  Now, how does this map to our source tree?
                    135: 
                    136: At the moment, the source test programs are located somewhere under src/tests/.
                    137: Say, for example, that you have the src/tests/bin/ls/ui_test.c source file.
                    138: This Makefile in src/tests/bin/ls/ will take this source file and generate a
                    139: ui_test binary.  The Makefile will also generate an Atffile.  Both files (the
                    140: ui_test binary and the Atffile) will later be installed to /usr/tests/bin/ls/
1.1       jmmv      141: 
1.7       jmmv      142: ## Executing a single test
                    143: 
                    144: In general, you **do not want to run a test program by hand**.  If you do so,
                    145: you do not take advantage of any of the isolation provided by the atf runtime.
                    146: This means that the test program will probably leave some temporary files behind
                    147: or will raise some false negatives.
                    148: 
                    149: To run a test, use atf-run.  In general:
                    150: 
                    151:     $ atf-run | atf-report  # To run all the test programs in a directory.
                    152:     $ atf-run some_test | atf-report  # To run only the some_test program.
                    153: 
                    154: The only "legitimate" case in which you should be running test cases by hand is
                    155: to debug them:
                    156: 
                    157:     $ gdb --args ./some_test the_broken_test_case
                    158: 
                    159: ... but be sure to clean up any leftover files if you do that.
                    160: 
                    161: ## Executing tests during development
                    162: 
                    163: When you are in a subdirectory of src/tests/, you can generally run "make test"
                    164: to execute the tests of that particular subdirectory.  This assumes that the
                    165: tests have been installed into the destdir.
                    166: 
                    167: Please note that this is only provided for convenience but it is completely
                    168: unsupported.  Tests run this way may fail misteriously, and that is perfectly
                    169: fine as long as they work from their canonical locations in /usr/tests.
                    170: 
1.5       jmmv      171: # Adding a new test
1.1       jmmv      172: 
                    173: To add a new *test case* to the source tree, look for any test program in
                    174: src/tests/ that can assimilate it.  If you find such a program, just add the
1.2       jmmv      175: test case to it: no other changes are required so your life is easy.  Otherwise,
                    176: you will have to create a new test program.
1.1       jmmv      177: 
                    178: To add a new *test program* to the source tree:
                    179: 
                    180: 1. Locate the appropriate subdirectory in which to put your test program.  It is
                    181: OK (and **expected**) to have multiple test programs into the same directory.
                    182: **Restrain yourself from creating one directory per test program.**
                    183: 
                    184: If the subdirectory exists:
                    185: 
                    186: 1. Choose a sane name for the test program; the name must not be so specific
                    187:    that it restricts the addition of future test cases into it.
                    188: 
                    189: 1. Create the test program source file using one of the templates below.
                    190:    E.g. src/tests/tutorial/sample_test.c.
                    191: 
                    192: 1. Add the new test program to the Makefile.
                    193: 
                    194: If the subdirectory does not exist:
                    195: 
                    196: 1. Do the same as above.
                    197: 
                    198: 1. Create the Makefile for the directory using the templates below.
                    199: 
                    200: 1. Edit the parent Makefile to recurse into the new subdirectory.
                    201: 
                    202: 1. Edit src/etc/mtree/NetBSD.base.dist to register the new subdirectory.  Your
                    203:    test will be installed under /usr/tests/.
                    204: 
                    205: 1. Edit src/distrib/sets/lists/tests/mi to register the new test program.  Do
                    206:    not forget to add .debug entries if your test program is a C/C++ binary.
                    207: 
1.5       jmmv      208: ## Makefile template
1.1       jmmv      209: 
1.7       jmmv      210: Follow this template to create your Makefile:
1.1       jmmv      211: 
                    212:     .include <bsd.own.mk>
                    213: 
                    214:     # This must always be defined.
                    215:     TESTSDIR= ${TESTSBASE}/bin/ls
                    216: 
1.7       jmmv      217:     # These correspond to the test programs you have in the directory.
1.1       jmmv      218:     TESTS_C+= c1_test c2_test  # Correspond to c1_test.c and c2_test.c.
                    219:     TESTS_SH+= sh1_test sh2_test  # Correspond to sh1_test.c and sh2_test.c
                    220: 
                    221:     # Define only if your tests need any data files.
                    222:     FILESDIR= ${TESTSDIR}
                    223:     FILES= testdata1.txt testdata2.bin  # Any necessary data files.
                    224: 
                    225:     .include <bsd.test.mk>
                    226: 
1.5       jmmv      227: ## Atffile template
1.2       jmmv      228: 
1.7       jmmv      229: *Atffiles are automatically generated by bsd.test.mk, so in general you will not
                    230: have to deal with them.*
                    231: 
1.2       jmmv      232: What is an Atffile?  An Atffile is the atf-run counterpart of a "Makefile".
                    233: Given that atf tests *do not rely on a toolchain*, they cannot use make(1) to
                    234: script their execution as the old tests in src/regress/ did.
                    235: 
                    236: The Atffiles, in general, just provide a list of test programs in a particular
                    237: directory and the list of the subdirectories to descend into.
                    238: 
1.7       jmmv      239: If you have to provide an Atffile explicitly because the automatic generation
                    240: does not suit your needs, follow this format:
1.2       jmmv      241: 
                    242:     Content-Type: application/X-atf-atffile; version="1"
                    243: 
                    244:     prop: test-suite = NetBSD
                    245: 
                    246:     tp: first_test
                    247:     tp: second_test
                    248:     tp-glob: optional_*_test
                    249:     tp: subdir1
                    250:     tp: subdir2
                    251: 
1.5       jmmv      252: # C test programs
1.1       jmmv      253: 
1.5       jmmv      254: ## Template
1.1       jmmv      255: 
1.7       jmmv      256: The following code snippet provides a C test program with two test cases.  The
                    257: specific details as to how this works follow later:
1.2       jmmv      258: 
1.1       jmmv      259:     #include <atf-c.h>
                    260: 
                    261:     ATF_TC(tc, my_test_case);
                    262:     ATF_TC_HEAD(tc, my_test_case)
                    263:     {
                    264:         atf_tc_set_md_var(tc, "descr", "This test case ensures that...");
                    265:     }
                    266:     ATF_TC_BODY(tc, my_test_case)
                    267:     {
1.7       jmmv      268:         ATF_CHECK(true); /* Success; continue execution. */
                    269:         ATF_CHECK(false); /* Failure; continue execution. */
1.1       jmmv      270: 
1.7       jmmv      271:         ATF_CHECK_EQ(5, 2 + 2); /* Failure; continue execution. */
                    272:         ATF_REQUIRE_EQ(5, 2 + 2); /* Failure; abort execution. */
1.1       jmmv      273: 
                    274:         if (!condition)
1.7       jmmv      275:             atf_tc_fail("Condition not met!"); /* Abort execution. */
1.1       jmmv      276:     }
                    277: 
1.9     ! jmmv      278:     ATF_TC(tc, another_test_case);
        !           279:     ATF_TC_HEAD(tc, another_test_case)
        !           280:     {
        !           281:         atf_tc_set_md_var(tc, "descr", "This test case ensures that...");
        !           282:     }
1.2       jmmv      283:     ATF_TC_BODY(tc, another_test_case)
                    284:     {
                    285:         /* Do more tests here... */
                    286:     }
                    287: 
1.1       jmmv      288:     ATF_TP_ADD_TCS(tp)
                    289:     {
                    290:         ATF_TP_ADD_TC(tp, my_test_case);
1.2       jmmv      291:         ATF_TP_ADD_TC(tp, another_test_case);
1.1       jmmv      292:     }
                    293: 
1.7       jmmv      294: This program needs to be built with the Makefile shown below.  Once built, the
                    295: program automatically gains a main() method that provides a consistent user
                    296: interface to all test programs.  You are simply not inteded to provide your own
                    297: main method, nor to deal with the command-line of the invocation.
1.2       jmmv      298: 
1.5       jmmv      299: ## How to build
1.1       jmmv      300: 
                    301: To build a C test program, append the name of the test program (without the .c
                    302: extension) to the TESTS_C variable in the Makefile.
                    303: 
                    304: For example:
                    305: 
                    306:     .include <bsd.own.mk>
                    307: 
                    308:     TESTSDIR= ${TESTSBASE}/bin/ls
                    309: 
                    310:     TESTS_C+= fs_test ui_test
                    311: 
                    312:     .include <bsd.test.mk>
                    313: 
1.5       jmmv      314: ## Common functions
1.3       jmmv      315: 
                    316: The following functions are commonly used from within a test case body:
                    317: 
                    318: * ATF_CHECK(boolean_expression): Checks if the given boolean expression is true
                    319:   and, if not, records a failure for the test case but *execution continues*.
                    320:   Using ATF_REQUIRE aborts execution immediately after a failure.
                    321: 
                    322: * ATF_CHECK_EQ(expected_expression, actual_expression): Checks if the two
                    323:   expressions match and, if not, records a failure.  Similarly, ATF_REQUIRE_EQ
                    324:   aborts immediately if the check fails.
                    325: 
                    326: * ATF_CHECK_STREQ(expected_string, actual_string): Same as ATF_CHECK_EQ but
                    327:   performs string comparisons with strcmp.
                    328: 
                    329: * atf_tc_skip(const char *format, ...): Marks the test case as skipped with the
                    330:   provided reason and exits.
                    331: 
                    332: * atf_tc_fail(const char *format, ...): Marks the test case as failed with the
                    333:   provided reason and exits.
                    334: 
                    335: * atf_tc_pass(void): Explicitly marks the test case as passed.  This is
                    336:   *implied* when the test case function ends, so you should not use this in
                    337:   general.
                    338: 
                    339: * atf_expect_fail(const char *format, ...): Tells the atf runtime that the code
                    340:   following this call is expected to raise one or more failures (be it with
                    341:   atf_tc_fail, ATF_CHECK_*, etc.).  Use this to mark a block of code that is
                    342:   known to be broken (e.g. a test that reproduces a known bug).  Use the string
                    343:   parameter to provide an explanation about why the code is broken; if possible,
                    344:   provide a PR number.  Lastly, to terminate the "expected failure" code block
                    345:   and reset the runtime to the default functionality, use the atf_expect_pass()
                    346:   function.
                    347: 
                    348: * atf_expect_death(const char *format, ...): Same as atf_expect_fail but expects
                    349:   an abrupt termination of the test case, be it due to a call to exit() or to
                    350:   the reception of a signal.
                    351: 
                    352: * atf_expect_exit(int exitcode, const char *fomat, ...): Same as atf_expect_fail
                    353:   but expects the test case to exit with a specific exitcode.  Provide -1 to
                    354:   indicate any exit code.
                    355: 
                    356: * atf_expect_signal(int signo, const char *fomat, ...): Same as atf_expect_fail
                    357:   but expects the test case to receive a specific signal.  Provide -1 to
                    358:   indicate any signal.
                    359: 
                    360: * atf_expect_timeout(const char *reason, ...): Same as atf_expect_fail but
                    361:   expects the test case to get stuck and time out.
                    362: 
1.8       jmmv      363: * atf_tc_get_config_var("srcdir"): Returns the path to the directory containing
                    364:   the test program binary.  This must be used to locate any data/auxiliary files
                    365:   stored alongside the binary.
                    366: 
1.5       jmmv      367: # Shell test programs
1.1       jmmv      368: 
1.5       jmmv      369: ## Template
1.1       jmmv      370: 
1.7       jmmv      371: The following code snippet provides a shell test program with two test cases.
                    372: The details on how this works are provided later:
1.2       jmmv      373: 
1.1       jmmv      374:     atf_test_case my_test_case
                    375:     my_test_case_head() {
                    376:         atf_set "descr" "This test case ensures that..."
                    377:     }
                    378:     my_test_case_body() {
                    379:         touch file1 file2
                    380: 
                    381:         cat >expout <<EOF
                    382:     file1
                    383:     file2
                    384:     EOF
1.7       jmmv      385:         # The following call validates that the 'ls' command returns an
                    386:         # exit code of 0, that its stdout matches exactly the contents
                    387:         # previously stored in the 'expout' file and that its stderr is
                    388:         # completely empty.  See atf-check(1) for details, which is the
                    389:         # auxiliary tool invoked by the atf_check wrapper function.
1.1       jmmv      390:         atf_check -s eq:0 -o file:expout -e empty 'ls'
                    391: 
                    392:         atf_check_equal 4 $((2 + 2))
                    393: 
                    394:         if [ 'a' != 'b' ]; then
                    395:             atf_fail "Condition not met!"  # Explicit failure.
                    396:         fi
                    397:     }
                    398: 
1.2       jmmv      399:     atf_test_case another_test_case
1.9     ! jmmv      400:     another_test_case_head() {
        !           401:         atf_set "descr" "This test case ensures that..."
        !           402:     }
1.2       jmmv      403:     another_test_case_body() {
                    404:         # Do more tests...
                    405:     }
                    406: 
1.1       jmmv      407:     atf_init_test_cases() {
                    408:         atf_add_test_case my_test_case
1.2       jmmv      409:         atf_add_test_case another_test_case
1.1       jmmv      410:     }
                    411: 
1.7       jmmv      412: This program needs to be built with the Makefile shown below.  The program
                    413: automatically gains an entry point that provides a consistent user interface to
                    414: all test programs.  You are simply not inteded to provide your own "main
                    415: method", nor to deal with the command-line of the invocation.
1.2       jmmv      416: 
1.5       jmmv      417: ## How to build
1.1       jmmv      418: 
                    419: To build a shell test program, append the name of the test program (without the
                    420: .sh extension) to the TESTS_SH variable in the Makefile.
                    421: 
                    422: For example:
                    423: 
                    424:     .include <bsd.own.mk>
                    425: 
                    426:     TESTSDIR= ${TESTSBASE}/bin/ls
                    427: 
                    428:     TESTS_SH+= integration_test something_else_test
                    429: 
                    430:     .include <bsd.test.mk>
                    431: 
                    432: If you want to run the test program yourself, you should know that shell-based
                    433: test programs are processed with the atf-sh interpreter.  atf-sh is just a thin
                    434: wrapper over /bin/sh that loads the shared atf code and then delegates execution
                    435: to your source file.
                    436: 
1.5       jmmv      437: ## Common functions
1.3       jmmv      438: 
                    439: The following functions are commonly used from within a test case body:
                    440: 
                    441: * atf_check: This is probably the most useful function for shell-based tests.
                    442:   It may need some experience to get it right, but it allows, in one line, to
                    443:   check the execution of a command.  Where check means: validate exit code,
                    444:   stdout and stderr.  This is just a wrapper over atf-check, so please refer to
                    445:   [atf-check(1)](http://netbsd.gw.com/cgi-bin/man-cgi?atf-check++NetBSD-current)
                    446:   for more details.
                    447: 
                    448: * atf_check_equal value1 value2: Check that the two values are equal and, if
                    449:   not, abort execution.
                    450: 
                    451: * atf_expect_*: Same as their C counterparts; see above.
                    452: 
                    453: * atf_fail reason: Explicitly marks the test case as failed and aborts it.
                    454: 
                    455: * atf_skip reason: Explicitly marks the test case as skipped and exits.
                    456: 
                    457: * atf_pass: Explicitly markts the test case as passed and exits.
                    458: 
                    459: * atf_get_srcdir: Prints the path to the directory where the test case lives.
                    460:   Use as $(atf_get_srcdir)/my-static-data-file.
                    461: 
1.5       jmmv      462: # FAQ
1.1       jmmv      463: 
1.5       jmmv      464: ## How do I atfify a plain test program?
1.1       jmmv      465: 
                    466: Let's suppose you have a program to exercise a particular piece of code.
                    467: Conceptually this implements a test but it does not use atf at all.  For
                    468: example:
                    469: 
                    470:     #include <err.h>
                    471:     #include <stdio.h>
                    472:     #include <stdlib.h>
                    473:     #include <string.h>
                    474: 
                    475:     /* This test program exercises the snprintf function. */
                    476: 
                    477:     int main(void)
                    478:     {
                    479:         char buf[1024];
                    480: 
                    481:         printf("Testing integers");
                    482:         snprintf(buf, sizeof(buf), "%d", 3);
                    483:         if (strcmp(buf, "3") != 0)
                    484:             errx(EXIT_FAILURE, "%d failed");
                    485:         snprintf(buf, sizeof(buf), "a %d b", 5);
                    486:         if (strcmp(buf, "a 5 b") != 0)
                    487:             errx(EXIT_FAILURE, "%d failed");
                    488: 
                    489:         printf("Testing strings");
                    490:         snprintf(buf, sizeof(buf), "%s", "foo");
                    491:         if (strcmp(buf, "foo") != 0)
                    492:             errx(EXIT_FAILURE, "%s failed");
                    493:         snprintf(buf, sizeof(buf), "a %s b", "bar");
                    494:         if (strcmp(buf, "a bar b") != 0)
                    495:             errx(EXIT_FAILURE, "%s failed");
                    496: 
                    497:         return EXIT_SUCCESS;
                    498:     }
                    499: 
                    500: To convert this program into an atf test program, use the template above and
                    501: keep this in mind:
                    502: 
                    503: * Split the whole main function into separate test cases.  In this scenario, the
                    504:   calls to printf(3) delimit a good granularity for the test cases: one for the
                    505:   integer formatter, one for the string formatter, etc.
                    506: 
                    507: * Use the ATF_CHECK* and/or atf_tc_fail functions to do the comparisons and
1.7       jmmv      508:   report errors.  Neither errx nor any other error reporting and program
                    509:   termination functions (read: err, errx, warn, warnx, exit, abor) are to be
                    510:   used at all.
1.1       jmmv      511: 
                    512: The result would look like:
                    513: 
                    514:     #include <atf-c.h>
                    515:     #include <stdio.h>
                    516: 
                    517:     ATF_TC(tc, integer_formatter);
                    518:     ATF_TC_HEAD(tc, integer_formatter)
                    519:     {
                    520:         atf_tc_set_md_var(tc, "descr", "Validates the %d formatter");
                    521:     }
                    522:     ATF_TC_BODY(tc, integer_formatter)
                    523:     {
                    524:         char buf[1024];
                    525: 
                    526:         snprintf(buf, sizeof(1024), "%d", 3);
                    527:         ATF_CHECK_STREQ("3", buf);
                    528: 
                    529:         snprintf(buf, sizeof(1024), "a %d b", 5);
                    530:         ATF_CHECK_STREQ("a 5 b", buf);
                    531:     }
                    532: 
                    533:     ATF_TC(tc, string_formatter);
                    534:     ATF_TC_HEAD(tc, string_formatter)
                    535:     {
                    536:         atf_tc_set_md_var(tc, "descr", "Validates the %s formatter");
                    537:     }
                    538:     ATF_TC_BODY(tc, string_formatter)
                    539:     {
                    540:         char buf[1024];
                    541: 
                    542:         snprintf(buf, sizeof(1024), "%s", "foo");
                    543:         ATF_CHECK_STREQ("foo", buf);
                    544: 
                    545:         snprintf(buf, sizeof(1024), "a %s b", "bar");
                    546:         ATF_CHECK_STREQ("a bar b", buf);
                    547:     }
                    548: 
                    549:     ATF_TP_ADD_TCS(tp)
                    550:     {
                    551:         ATF_TP_ADD_TC(tp, integer_formatter);
                    552:         ATF_TP_ADD_TC(tp, string_formatter);
                    553:     }
                    554: 
                    555: Which can later be invoked as any of:
                    556: 
1.7       jmmv      557:     $ atf-run snprintf_test | atf-report  # Normal execution method.
                    558:     $ ./snprintf_test integer_formatter  # For DEBUGGING only.
                    559:     $ ./snprintf_test string_formatter  # For DEBUGGING only.
1.1       jmmv      560: 
1.5       jmmv      561: ## How do I write a test case for an unfixed PR?
1.3       jmmv      562: 
                    563: Use the "expectations" mechanism to define part of the test case as faulty,
1.7       jmmv      564: crashy, etc.  This is for two reasons:
                    565: 
                    566: * As long as the bug still exists, he test case will be reported as an "expected
                    567:   failure".  Such expected failures do not count towards the success or failure
                    568:   of the whole test suite.
                    569: 
                    570: * When the bug gets fixed, the bug will not trigger any more in the test case,
                    571:   and thus the expectation of failure will not be met any more.  At this point
                    572:   the test case will start raising a regular failure, which is usually addressed
                    573:   by either just removing the expect_* calls.
                    574: 
                    575: For example, suppose we have PR lib/1 that reports a condition in which
                    576: snprintf() does the wrong formatting when using %s, and PR lib/2 that mentions
1.3       jmmv      577: that another snprintf() call using %d with number 5 causes a segfault.  We could
                    578: do:
                    579: 
                    580:     #include <atf-c.h>
                    581:     #include <signal.h>
                    582:     #include <stdio.h>
                    583: 
1.9     ! jmmv      584:     ATF_TC(tc, integer_formatter);
        !           585:     ATF_TC_HEAD(tc, integer_formatter)
        !           586:     {
        !           587:         atf_tc_set_md_var(tc, "descr", "Tests the %d formatter for snprintf");
        !           588:     }
1.3       jmmv      589:     ATF_TC_BODY(tc, integer_formatter)
                    590:     {
                    591:         char buf[1024];
                    592: 
                    593:         snprintf(buf, sizeof(buf), "Hello %d\n", 1);
                    594:         ATF_CHECK_STREQ("Hello 1", buf);
                    595: 
1.7       jmmv      596:         atf_tc_expect_signal(SIGSEGV, "PR lib/2: %%d with 5 causes a crash");
1.3       jmmv      597:         snprintf(buf, sizeof(buf), "Hello %d\n", 5);
                    598:         atf_tc_expect_pass();
                    599:         ATF_CHECK_STREQ("Hello 5", buf);
                    600:     }
                    601: 
1.9     ! jmmv      602:     ATF_TC(tc, string_formatter);
        !           603:     ATF_TC_HEAD(tc, string_formatter)
        !           604:     {
        !           605:         atf_tc_set_md_var(tc, "descr", "Tests the %s formatter for snprintf");
        !           606:     }
1.3       jmmv      607:     ATF_TC_BODY(tc, string_formatter)
                    608:     {
                    609:         char buf[1024];
                    610: 
                    611:         snprintf(buf, sizeof(buf), "Hello %s\n", "world!");
1.7       jmmv      612:         atf_tc_expect_failure("PR lib/1: %%s does not work");
1.3       jmmv      613:         ATF_CHECK_STREQ("Hello world!", buf);
                    614:         atf_tc_expect_pass();
                    615:     }
                    616: 
                    617:     ATF_TP_ADD_TCS(tp)
                    618:     {
                    619:         ATF_TP_ADD_TC(tp, integer_formatter);
                    620:         ATF_TP_ADD_TC(tp, string_formatter);
                    621:     }
                    622: 
1.5       jmmv      623: ## Do I need to remove temporary files?
1.1       jmmv      624: 
                    625: No.  atf-run does this automatically for you, because it runs every test program
                    626: in its own temporary subdirectory.
1.7       jmmv      627: 
                    628: ## When do I use ATF_CHECK and when ATF_REQUIRE?
                    629: 
                    630: ATF_CHECK logs errors but does not abort the execution of the test program.
                    631: ATF_REQUIRE logs errors in a similar way but immediately terminates the
                    632: execution.
                    633: 
                    634: You can use this distinction in the following way: use ATF_REQUIRE to check the
                    635: code that "prepares" your test case.  Use ATF_CHECK to do the actual
                    636: functionality tests once all the set up has been performed.  For example:
                    637: 
                    638:     ATF_TC_BODY(getline) {
                    639:         FILE *f;
                    640:         char buf[1024];
                    641: 
                    642:         /* Opening the file is not part of the functionality under test, but it
                    643:          * must succeed before we actually test the relevant code. */
                    644:         ATF_REQUIRE((f = fopen("foo")) != NULL);
                    645: 
                    646:         ATF_CHECK(getline(f, buf, sizeof(buf)) > 0);
                    647:         ATF_CHECK_STREQ("line 1", buf);
                    648: 
                    649:         ATF_CHECK(getline(f, buf, sizeof(buf)) > 0);
                    650:         ATF_CHECK_STREQ("line 2", buf);
                    651:     }

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb