1Writing APR tests 2 3All APR tests should be executable in 2 ways, as an individual program, or 4as a part of the full test suite. The full test suite is controlled with 5the testall program. At the beginning of the testall.c file, there is an 6array of functions called tests. The testall program loops through this 7array calling each function. Each function returns a CuSuite variable, which 8is then added to the SuiteList. Once all Suites have been added, the SuiteList 9is executed, and the output is printed to the screen. All functions in the 10array should follow the same basic format: 11 12The Full Suite 13-------------- 14 15/* The driver function. This must return a CuSuite variable, which will 16 * then be used to actually run the tests. Essentially, all Suites are a 17 * collection of tests. The driver will take each Suite, and put it in a 18 * SuiteList, which is a collection of Suites. 19 */ 20CuSuite *testtime(void) 21{ 22 /* The actual suite, this must be created for each test program. Please 23 * give it a useful name, that will inform the user of the feature being 24 * tested. 25 */ 26 CuSuite *suite = CuSuiteNew("Test Time"); 27 28 /* Each function must be added to the suite. Each function represents 29 * a single test. It is possible to test multiple features in a single 30 * function, although no tests currently do that. 31 */ 32 SUITE_ADD_TEST(suite, test_now); 33 SUITE_ADD_TEST(suite, test_gmtstr); 34 SUITE_ADD_TEST(suite, test_localstr); 35 SUITE_ADD_TEST(suite, test_exp_get_gmt); 36 SUITE_ADD_TEST(suite, test_exp_get_lt); 37 SUITE_ADD_TEST(suite, test_imp_gmt); 38 SUITE_ADD_TEST(suite, test_rfcstr); 39 SUITE_ADD_TEST(suite, test_ctime); 40 SUITE_ADD_TEST(suite, test_strftime); 41 SUITE_ADD_TEST(suite, test_strftimesmall); 42 SUITE_ADD_TEST(suite, test_exp_tz); 43 SUITE_ADD_TEST(suite, test_strftimeoffset); 44 45 /* You must return the suite so that the driver knows which suites to 46 * run. 47 */ 48 return suite; 49} 50 51Building the full driver 52------------------------ 53 54All you need to do to build the full driver is run: 55 56 make 57 58To run it, run: 59 60 ./testall 61 62Running individual tests 63------------------------ 64 65It is not possible to build individual tests, however it is possible to 66run individual tests. When running the test suite, specify the name of the 67tests that you want to run on the command line. For example: 68 69 ./testall teststr testrand 70 71Will run the Strings and Random generator tests. 72 73Reading the test suite output 74----------------------------- 75 76Once you run the test suite, you will get output like: 77 78All APR Tests: 79 Test Strings: .... 80 Test Time: ............ 81 8216 tests run: 16 passed, 0 failed, 0 not implemented. 83 84Known test failures are documented in ../STATUS. 85 86There are a couple of things to look at with this. First, if you look at the 87first function in this document, you should notice that the string passed to 88the CuSuiteNew function is in the output. That is why the string should 89explain the feature you are testing. 90 91Second, this test passed completely. This is obvious in two ways. First, and 92most obvious, the summary line tells you that 16 tests were run and 16 tests 93passed. However, the results can also be found in the lines above. Every 94'.' in the output represents a passed test. 95 96If a test fails, the output will look like: 97 98All APR Tests: 99 Test Strings: .... 100 Test Time: ..F......... 101 10216 tests run: 15 passed, 1 failed, 0 not implemented. 103 104This is not very useful, because you don't know which test failed. However, 105once you know that a test failed, you can run the suite again, with the 106-v option. If you do this, you will get something like: 107 108All APR Tests: 109 Test Strings: .... 110 Test Time: ..F......... 111 11216 tests run: 15 passed, 1 failed, 0 not implemented. 113Failed tests: 1141) test_localstr: assert failed 115 116In this case, we know the test_localstr function failed, and there is an 117Assert in this that failed (I modified the test to fail for this document). 118Now, you can look at what that test does, and why it would have failed. 119 120There is one other possible output for the test suite (run with -v): 121 122All APR Tests: 123 Test Strings: .... 124 Test Time: ..N......... 125 12616 tests run: 15 passed, 0 failed, 1 not implemented. 127 128Not Implemented tests: 129 130Not Implemented tests: 1311) test_localstr: apr_time_exp_lt not implemented on this platform 132 133The 'N' means that a function has returned APR_ENOTIMPL. This should be 134treated as an error, and the function should be implemented as soon as 135possible. 136 137Adding New test Suites to the full driver 138------------------------------------------- 139 140To add a new Suite to the full driver, you must make a couple of modifications. 141 1421) Edit test_apr.h, and add the prototype for the function. 1432) Edit testall.c, and add the function and name to the tests array. 1443) Edit Makefile.in, and add the .lo file to the testall target. 145 146Once those four things are done, your tests will automatically be added 147to the suite. 148 149Writting an ABTS unit test 150-------------------------- 151 152The aim of this quick and dirty Howto is to give a short introduction 153to APR (Apache Portable Runtime) unit tests, and how to write 154one. During my Google's Summer of Code 2005 project, I discovered a 155small bug in the APR-Util's date parsing routines, and I needed to 156write a unit test for the fixed code. I decided to write this 157documentation because I did not find any. Thanks to Garrett Rooney for 158his help on writing the unit test ! 159 160The APR and APR-Util libraries provide a platform independent API for 161software developers. They contain a lot of modules, including network 162programming, threads, string and memory management, etc. All these 163functions need to be heavily tested so that developers can be sure the 164library is reliable. 165 166The ABTS give APR developers the ability to build a complete test 167suite for the bunch of tests they wrote, which can then be ran under 168various platforms. In this Howto, I will try teach you how to write an 169ABTS unit test. 170 171As you may probably know, a unit test is a simple routine which tests 172a very specific feature of the tested software or library. To build a 173unit test, you need three different things : 174 175 * the to-be-tested function, 176 * the input data that will be given to the function, 177 * the expected output data. 178 179The principle of a unit test is very simple : for each entry in your 180set of input data, we pass it to our function, fetch what the function 181returned and compare it to the corresponding expected output data. Of 182course, the more edge cases you can test, the better your input data 183set is. 184 185The ABTS aims to quicken the write of unit test, and make them 186available to the whole test suite by providing a set of preprocessor 187macros. Adding a unit test to a test suite can be easily done by the 188following piece of code : 189 190abts_suite *testdaterfc(abts_suite *suite) 191{ 192 suite = ADD_SUITE(suite); 193 abts_run_test(suite, test_date_rfc, NULL); 194 195 return suite; 196} 197 198Where test_date_rfc is the name of the function performing the 199test. Writing such a function is, in the light of the explanation I 200just gave, pretty much easy too. As I said, we need to check every 201entry of our input data set. That gives us a loop. For each loop 202iteration, we call our to-be-tested function, grab its result and 203compare the returned value with the expected one. 204 205Test functions must have the following prototype : 206 207static void my_test_function(abts_case *tc, void *data); 208 209The comparison step is performed by the ABTS, thus giving the 210whole test suite the correct behavior if your unit test fails. Here 211comes a list of the available test methods : 212 213ABTS_INT_EQUAL(tc, a, b) 214ABTS_INT_NEQUAL(tc, a, b) 215ABTS_STR_EQUAL(tc, a, b) 216ABTS_STR_NEQUAL(tc, a, b, c) 217ABTS_PTR_NOTNULL(tc, b) 218ABTS_PTR_EQUAL(tc, a, b) 219ABTS_TRUE(tc, b) 220ABTS_FAIL(tc, b) 221ABTS_NOT_IMPL(tc, b) 222ABTS_ASSERT(tc, a, b) 223 224The first argument, tc is a reference to the unit test currently 225processed by the test suite (passed to your test function). The other 226parameters are the data to be tested. For example, the following line 227will never make your unit test fail : 228 229ABTS_INT_EQUAL(tc, 1, 1); 230 231See, it's easy ! Let's take a look at the complete example : 232testdaterfc. We want to test our date string parser. For this, we will 233use some chosen date strings (from mail headers for example) written 234in various formats but that should all be handled by our function, and 235their equivalents in correct RFC822 format. 236 237The function we want to test returns an apr_time_t}, which will be 238directly given as input to the apr_rfc822_date() function, thus 239producing the corresponding RFC822 date string. All we need to do 240after this is to call the correct test method from the ABTS macros ! 241 242You can take a look at the apr-util/test/testdaterfc.c file for the 243complete source code of this unit test. 244 245Although this Howto is very small and mostly dedicated to the 246testdaterfc unit test, I hope you'll find it useful. Good luck ! 247 248Writing tests for CuTest (no longer used) 249----------------------------------------- 250 251There are a couple of rules for writing good tests for the test suite. 252 2531) All tests can determine for themselves if it passed or not. This means 254that there is no reason for the person running the test suite to interpret 255the results of the tests. 2562) Never use printf to add to the output of the test suite. The suite 257library should be able to print all of the information required to debug 258a problem. 2593) Functions should be tested with both positive and negative tests. This 260means that you should test things that should both succeed and fail. 2614) Just checking the return code does _NOT_ make a useful test. You must 262check to determine that the test actually did what you expected it to do. 263 264An example test 265--------------- 266 267Finally, we will look at a quick test: 268 269/* All tests are passed a CuTest variable. This is how the suite determines 270 * if the test succeeded or failed. 271 */ 272static void test_localstr(CuTest *tc) 273{ 274 apr_status_t rv; 275 apr_time_exp_t xt; 276 time_t os_now; 277 278 rv = apr_time_exp_lt(&xt, now); 279 os_now = now / APR_USEC_PER_SEC; 280 281 /* If the function can return APR_ENOTIMPL, then you should check for it. 282 * This allows platform implementors to know if they have to implement 283 * the function. 284 */ 285 if (rv == APR_ENOTIMPL) { 286 CuNotImpl(tc, "apr_time_exp_lt"); 287 } 288 289 /* It often helps to ensure that the return code was APR_SUCESS. If it 290 * wasn't, then we know the test failed. 291 */ 292 CuAssertTrue(tc, rv == APR_SUCCESS); 293 294 /* Now that we know APR thinks it worked properly, we need to check the 295 * output to ensure that we got what we expected. 296 */ 297 CuAssertStrEquals(tc, "2002-08-14 12:05:36.186711 -25200 [257 Sat] DST", 298 print_time(p, &xt)); 299} 300 301Notice, the same test can fail for any of a number of reasons. The first 302test to fail ends the test. 303 304CuTest 305------ 306 307CuTest is an open source test suite written by Asim Jalis. It has been 308released under the zlib/libpng license. That license can be found in the 309CuTest.c and CuTest.h files. 310 311The version of CuTest that is included in the APR test suite has been modified 312from the original distribution in the following ways: 313 3141) The original distribution does not have a -v flag, the details are always 315printed. 3162) The NotImplemented result does not exist. 3173) SuiteLists do not exist. In the original distribution, you can add suites 318to suites, but it just adds the tests in the first suite to the list of tests 319in the original suite. The output wasn't as detailed as I wanted, so I created 320SuiteLists. 321 322The first two modifications have been sent to the original author of CuTest, 323but they have not been integrated into the base distribution. The SuiteList 324changes will be sent to the original author soon. 325 326The modified version of CuTest is not currently in any CVS or Subversion 327server. In time, it will be hosted at rkbloom.net. 328 329There are currently no docs for how to write tests, but the teststr and 330testtime programs should give an idea of how it is done. In time, a document 331should be written to define how tests are written. 332 333