README 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. EGlib Unit Testing
  2. ===============================================================================
  3. 1. Writing new tests
  4. 2. Using the test driver
  5. ===============================================================================
  6. 1. Writing new tests
  7. ===============================================================================
  8. Tests are easy to write, but must be grouped in to logical cases. For instance,
  9. the GPtrArray group has a number of tests that cover the entire GPtrArray
  10. implementation.
  11. These logical case groups should be in a single C file, and must have
  12. three elements:
  13. #include <glib.h>
  14. #include "test.h"
  15. ...
  16. <define test implementations>
  17. ...
  18. static Test groupname_tests [] = {
  19. {"groupname_test1", groupname_test1},
  20. {"groupname_test1", groupname_test2},
  21. {NULL, NULL}
  22. };
  23. DEFINE_TEST_GROUP_INIT(groupname_tests_init, groupname_tests)
  24. A test implementation should look like:
  25. RESULT groupname_test1()
  26. {
  27. <perform the test>
  28. if(test_failed) {
  29. return FAILED("reason: %s", "this works like printf");
  30. }
  31. return OK; /* just NULL, but OK is cute */
  32. }
  33. Once a test group is written, it needs to be added to the groups table
  34. in tests.h:
  35. DEFINE_TEST_GROUP_INIT_H(groupname_tests_init) // same as in impl
  36. static Group test_groups [] = {
  37. ...
  38. {"groupname", groupname_tests_init}
  39. ...
  40. };
  41. ===============================================================================
  42. 2. Using the test driver
  43. ===============================================================================
  44. When tests are written, they are rebuilt with make. Two programs will be
  45. built:
  46. test-eglib: the test driver and tests linked against eglib
  47. test-glib: the test driver and tests linked against system glib-2.0
  48. Each driver program works exactly the same. Running test-eglib will run
  49. the tests against eglib, and test-glib against glib-2.0.
  50. The test driver supports a few options to allow for performance measuring:
  51. --help show all options and available test groups
  52. --time time the overall run and report it, even if --quiet is set
  53. --quiet do not print test results, useful for timing
  54. --iterations N run all or specified test groups N times
  55. Run "test-eglib --help" for more details.
  56. Example: run the ptrarray test group 100000 times and only print the time
  57. it took to perform all iterations
  58. ./test-eglib -tqi 100000 ptrarray
  59. Example: show single iteration of test output for two groups
  60. ./test-eglib ptrarray hashtable
  61. Example: show test output of all available groups
  62. ./test-eglib
  63. The 'test-both' script can be used to run both test-eglib and test-glib
  64. with the same options back to back:
  65. $ ./test-both -tqi 100000 ptrarray
  66. EGlib Total Time: 1.1961s
  67. GLib Total Time: 0.955957s
  68. test-both also has a nice --speed-compare mode that shows comparison
  69. information about EGlib vs GLib. It can run all tests or specific tests
  70. with a configurable number of iterations. --speed-compare mode always runs
  71. the drivers with -qtni
  72. The syntax for --speed-compare is:
  73. ./test-both --speed-compare [ITERATIONS] [GROUPS...]
  74. $ ./test-both --speed-compare Runs all tests with default iterations
  75. $ ./test-both --speed-compare 500 Runs all tests with 500 iterations
  76. $ ./test-both --speed-compare ptrarray Runs ptrarray test with default
  77. iterations