2
0

README 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. Writing APR tests
  2. All APR tests should be executable in 2 ways, as an individual program, or
  3. as a part of the full test suite. The full test suite is controlled with
  4. the testall program. At the beginning of the testall.c file, there is an
  5. array of functions called tests. The testall program loops through this
  6. array calling each function. Each function returns a CuSuite variable, which
  7. is then added to the SuiteList. Once all Suites have been added, the SuiteList
  8. is executed, and the output is printed to the screen. All functions in the
  9. array should follow the same basic format:
  10. The Full Suite
  11. --------------
  12. /* The driver function. This must return a CuSuite variable, which will
  13. * then be used to actually run the tests. Essentially, all Suites are a
  14. * collection of tests. The driver will take each Suite, and put it in a
  15. * SuiteList, which is a collection of Suites.
  16. */
  17. CuSuite *testtime(void)
  18. {
  19. /* The actual suite, this must be created for each test program. Please
  20. * give it a useful name, that will inform the user of the feature being
  21. * tested.
  22. */
  23. CuSuite *suite = CuSuiteNew("Test Time");
  24. /* Each function must be added to the suite. Each function represents
  25. * a single test. It is possible to test multiple features in a single
  26. * function, although no tests currently do that.
  27. */
  28. SUITE_ADD_TEST(suite, test_now);
  29. SUITE_ADD_TEST(suite, test_gmtstr);
  30. SUITE_ADD_TEST(suite, test_localstr);
  31. SUITE_ADD_TEST(suite, test_exp_get_gmt);
  32. SUITE_ADD_TEST(suite, test_exp_get_lt);
  33. SUITE_ADD_TEST(suite, test_imp_gmt);
  34. SUITE_ADD_TEST(suite, test_rfcstr);
  35. SUITE_ADD_TEST(suite, test_ctime);
  36. SUITE_ADD_TEST(suite, test_strftime);
  37. SUITE_ADD_TEST(suite, test_strftimesmall);
  38. SUITE_ADD_TEST(suite, test_exp_tz);
  39. SUITE_ADD_TEST(suite, test_strftimeoffset);
  40. /* You must return the suite so that the driver knows which suites to
  41. * run.
  42. */
  43. return suite;
  44. }
  45. Building the full driver
  46. ------------------------
  47. All you need to do to build the full driver is run:
  48. make testall
  49. To run it, run:
  50. ./testall
  51. Running individual tests
  52. ---------------------------------
  53. It is not possible to build individual tests, however it is possible to
  54. run individual tests. When running the test suite, specify the name of the
  55. tests that you want to run on the command line. For example:
  56. ./testall teststr testrand
  57. Will run the Strings and Random generator tests.
  58. Reading the test suite output
  59. -----------------------------
  60. Once you run the test suite, you will get output like:
  61. All APR Tests:
  62. Test Strings: ....
  63. Test Time: ............
  64. 16 tests run: 16 passed, 0 failed, 0 not implemented.
  65. Known test failures are documented in ../STATUS.
  66. There are a couple of things to look at with this. First, if you look at the
  67. first function in this document, you should notice that the string passed to
  68. the CuSuiteNew function is in the output. That is why the string should
  69. explain the feature you are testing.
  70. Second, this test passed completely. This is obvious in two ways. First, and
  71. most obvious, the summary line tells you that 16 tests were run and 16 tests
  72. passed. However, the results can also be found in the lines above. Every
  73. '.' in the output represents a passed test.
  74. If a test fails, the output will look like:
  75. All APR Tests:
  76. Test Strings: ....
  77. Test Time: ..F.........
  78. 16 tests run: 15 passed, 1 failed, 0 not implemented.
  79. This is not very useful, because you don't know which test failed. However,
  80. once you know that a test failed, you can run the suite again, with the
  81. -v option. If you do this, you will get something like:
  82. All APR Tests:
  83. Test Strings: ....
  84. Test Time: ..F.........
  85. 16 tests run: 15 passed, 1 failed, 0 not implemented.
  86. Failed tests:
  87. 1) test_localstr: assert failed
  88. In this case, we know the test_localstr function failed, and there is an
  89. Assert in this that failed (I modified the test to fail for this document).
  90. Now, you can look at what that test does, and why it would have failed.
  91. There is one other possible output for the test suite (run with -v):
  92. All APR Tests:
  93. Test Strings: ....
  94. Test Time: ..N.........
  95. 16 tests run: 15 passed, 0 failed, 1 not implemented.
  96. Not Implemented tests:
  97. Not Implemented tests:
  98. 1) test_localstr: apr_time_exp_lt not implemented on this platform
  99. The 'N' means that a function has returned APR_ENOTIMPL. This should be
  100. treated as an error, and the function should be implemented as soon as
  101. possible.
  102. Adding New test Suites to the full driver
  103. -------------------------------------------
  104. To add a new Suite to the full driver, you must make a couple of modifications.
  105. 1) Edit test_apr.h, and add the prototype for the function.
  106. 2) Edit testall.c, and add the function and name to the tests array.
  107. 3) Edit Makefile.in, and add the .lo file to the testall target.
  108. Once those four things are done, your tests will automatically be added
  109. to the suite.
  110. Writing tests
  111. -------------
  112. There are a couple of rules for writing good tests for the test suite.
  113. 1) All tests can determine for themselves if it passed or not. This means
  114. that there is no reason for the person running the test suite to interpret
  115. the results of the tests.
  116. 2) Never use printf to add to the output of the test suite. The suite
  117. library should be able to print all of the information required to debug
  118. a problem.
  119. 3) Functions should be tested with both positive and negative tests. This
  120. means that you should test things that should both succeed and fail.
  121. 4) Just checking the return code does _NOT_ make a useful test. You must
  122. check to determine that the test actually did what you expected it to do.
  123. An example test
  124. ---------------
  125. Finally, we will look at a quick test:
  126. /* All tests are passed a CuTest variable. This is how the suite determines
  127. * if the test succeeded or failed.
  128. */
  129. static void test_localstr(CuTest *tc)
  130. {
  131. apr_status_t rv;
  132. apr_time_exp_t xt;
  133. time_t os_now;
  134. rv = apr_time_exp_lt(&xt, now);
  135. os_now = now / APR_USEC_PER_SEC;
  136. /* If the function can return APR_ENOTIMPL, then you should check for it.
  137. * This allows platform implementors to know if they have to implement
  138. * the function.
  139. */
  140. if (rv == APR_ENOTIMPL) {
  141. CuNotImpl(tc, "apr_time_exp_lt");
  142. }
  143. /* It often helps to ensure that the return code was APR_SUCESS. If it
  144. * wasn't, then we know the test failed.
  145. */
  146. CuAssertTrue(tc, rv == APR_SUCCESS);
  147. /* Now that we know APR thinks it worked properly, we need to check the
  148. * output to ensure that we got what we expected.
  149. */
  150. CuAssertStrEquals(tc, "2002-08-14 12:05:36.186711 -25200 [257 Sat] DST",
  151. print_time(p, &xt));
  152. }
  153. Notice, the same test can fail for any of a number of reasons. The first
  154. test to fail ends the test.
  155. CuTest
  156. ------
  157. CuTest is an open source test suite written by Asim Jalis. It has been
  158. released under the zlib/libpng license. That license can be found in the
  159. CuTest.c and CuTest.h files.
  160. The version of CuTest that is included in the APR test suite has been modified
  161. from the original distribution in the following ways:
  162. 1) The original distribution does not have a -v flag, the details are always
  163. printed.
  164. 2) The NotImplemented result does not exist.
  165. 3) SuiteLists do not exist. In the original distribution, you can add suites
  166. to suites, but it just adds the tests in the first suite to the list of tests
  167. in the original suite. The output wasn't as detailed as I wanted, so I created
  168. SuiteLists.
  169. The first two modifications have been sent to the original author of CuTest,
  170. but they have not been integrated into the base distribution. The SuiteList
  171. changes will be sent to the original author soon.
  172. The modified version of CuTest is not currently in any CVS or Subversion
  173. server. In time, it will be hosted at rkbloom.net.
  174. There are currently no docs for how to write tests, but the teststr and
  175. testtime programs should give an idea of how it is done. In time, a document
  176. should be written to define how tests are written.