tn0017.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. How to write unit tests for wxWidgets
  2. =====================================
  3. Unit tests for wxWidgets are written using small cppunit framework. To compile
  4. (but not to run) them you need to have it installed. Hence the first part of
  5. this note explains how to do it while the second one explains how to write the
  6. test.
  7. I. CppUnit Installation
  8. -----------------------
  9. 1. Get it from http://www.sourceforge.net/projects/cppunit
  10. (latest version as of the time of this writing is 1.10.2)
  11. 2. Build the library:
  12. a) Under Windows using VC++ (versions 6, 7, 8 & 9 work):
  13. - build everything in CppUnitLibraries.dsw work space
  14. - add include and lib subdirectories of the directory
  15. where you installed cppunit to the compiler search path
  16. using "Tools|Options" menu in VC IDE
  17. b) Under Unix: run configure && make && make install as usual
  18. II. Writing tests with CppUnit
  19. ------------------------------
  20. 1. Create a new directory tests/foo
  21. 2. Write a cpp file for the test copying, if you want,
  22. from one of the existing tests. The things to look for:
  23. a) #include "wx/cppunit.h" instead of directly including CppUnit headers
  24. b) don't put too many things in one test case nor in one method of a test
  25. case as it makes understanding what exactly failed harder later
  26. c) 'register' your tests as follows so that the test program will find and
  27. execute them:
  28. // register in the unnamed registry so that these tests are run by default
  29. CPPUNIT_TEST_SUITE_REGISTRATION(MBConvTestCase);
  30. // also include in its own registry so that these tests can be run alone
  31. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MBConvTestCase, "MBConvTestCase");
  32. Read CppUnit documentation for more.
  33. d) wxUIActionSimulator can be used when user input is required, for example
  34. clicking buttons or typing text. A simple example of this can be found
  35. in controls/buttontest.cpp. After simulating some user input always
  36. wxYield to allow event processing. When writing a test using
  37. wxUIActionSimulator always add the test using WXUISIM_TEST rather than
  38. CPPUNIT_TEST as then it won't run on unsupported platforms. The test itself
  39. must also be wrapped in a #if wxUSE_UIACTIONSIMULATOR block.
  40. e) There are a number of classes that are available to help with testing GUI
  41. elements. Firstly throughout the test run there is a frame of type
  42. wxTestableFrame that you can access through wxTheApp->GetTopWindow(). This
  43. class adds two new functions, GetEventCount, which takes an optional
  44. wxEventType. It then returns the number of events of that type that it has
  45. received since the last call. Passing nothing returns the total number of
  46. event received since the last call. Also there is OnEvent, which counts the
  47. events based on type that are passed to it. To make it easy to count events
  48. there is also a new class called EventCounter which takes a window and event
  49. type and connects the window to the top level wxTestableFrame with the specific
  50. event type. It disconnects again once it is out of scope. It simply reduces
  51. the amount of typing required to count events.
  52. 3. add a '<sources>' tag for your source file to tests/test.bkl. Make sure it's
  53. in the correct section: the one starting '<exe id="test_gui"' for a gui test,
  54. the one starting '<exe id="test" template="wx_sample_console' otherwise.
  55. III. Running the tests
  56. ----------------------
  57. 1. Regenerate the make/project files from test.bkl using bakefile_gen, e.g.:
  58. cd build/bakefiles
  59. bakefile_gen -b ../../tests/test.bkl
  60. and if you're on a unix system re-run configure.
  61. 2. Build the test program using one of the make/project files in the tests
  62. subdirectory.
  63. 3. Run the test program by using the command 'test' for the console tests,
  64. 'test_gui' for the gui ones. With no arguments, all the default set of tests
  65. (all those registered with CPPUNIT_TEST_SUITE_REGISTRATION) are run.
  66. Or to list the test suites without running them:
  67. test -l or test_gui -l
  68. 4. Tests that have been registered under a name using
  69. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION can also be run separately. For
  70. example:
  71. test_gui ButtonTestCase
  72. or to list the tests done by a particular testcase:
  73. test -L MBConvTestCase
  74. 5. Fault navigation.
  75. VC++ users can run the programs as a post build step (Projects/Settings/
  76. Post-build step) to see the test results in an IDE window. This allows
  77. errors to be jumped to in the same way as for compiler errors, for
  78. example by pressing F4 or highlighting the error and pressing return.
  79. Similarly for makefile users: makefiles can be modified to execute the
  80. test programs as a final step. Then you can navigate to any errors in the
  81. same way as for compiler errors, if your editor supports that.
  82. Another alternative is to run the tests manually, redirecting the output
  83. to a file. Then use your editor to jump to any failures. Using Vim, for
  84. example, ':cf test.log' would take you to the first error in test.log, and
  85. ':cn' to the next.
  86. If you would like to set a breakpoint on a failing test using a debugger,
  87. put the breakpoint on the function 'CppUnit::Asserter::fail()'. This will
  88. stop on each failing test.
  89. IV. Notes
  90. ---------
  91. 1. You can register your tests (or a subset of them) just under a name, and not
  92. in the unnamed registry if you don't want them to be executed by default.
  93. 2. If you are going to register your tests both in the unnamed registry
  94. and under a name, then use the name that the tests have in the 'test -l'
  95. listing.
  96. 3. Tests which fail can be temporarily registered under "fixme" while the
  97. problems they expose are fixed, instead of the unnamed registry. That
  98. way they can easily be run, but they do not make regression testing with
  99. the default suite more difficult. E.g.:
  100. // register in the unnamed registry so that these tests are run by default
  101. //CPPUNIT_TEST_SUITE_REGISTRATION(wxRegExTestCase);
  102. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(wxRegExTestCase, "fixme");
  103. // also include in its own registry so that these tests can be run alone
  104. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(wxRegExTestCase, "wxRegExTestCase");
  105. 4. Tests which take a long time to execute can be registered under "advanced"
  106. instead of the unnamed registry. The default suite should execute reasonably
  107. quickly. To run the default and advanced tests together:
  108. test "" advanced
  109. === EOF ===
  110. Author: VZ & MW