How to write unit tests for wxWidgets ===================================== Unit tests for wxWidgets are written using small cppunit framework. To compile (but not to run) them you need to have it installed. Hence the first part of this note explains how to do it while the second one explains how to write the test. I. CppUnit Installation ----------------------- 1. Get it from http://www.sourceforge.net/projects/cppunit (latest version as of the time of this writing is 1.10.2) 2. Build the library: - Under Windows using VC++: - build everything in CppUnitLibraries.dsw work space - add include and lib subdirectories of the directory where you installed cppunit to the compiler search path using "Tools|Options" menu in VC IDE - Under Unix: run `configure && make && make install` as usual II. Writing tests with CppUnit ------------------------------ 1. Create a new directory tests/foo 2. Write a cpp file for the test copying, if you want, from one of the existing tests. The things to look for: a) #include "wx/cppunit.h" instead of directly including CppUnit headers b) don't put too many things in one test case nor in one method of a test case as it makes understanding what exactly failed harder later c) 'register' your tests as follows so that the test program will find and execute them: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION(MBConvTestCase); // also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(MBConvTestCase, "MBConvTestCase"); Read CppUnit documentation for more. d) wxUIActionSimulator can be used when user input is required, for example clicking buttons or typing text. A simple example of this can be found in controls/buttontest.cpp. After simulating some user input always wxYield to allow event processing. When writing a test using wxUIActionSimulator always add the test using WXUISIM_TEST rather than CPPUNIT_TEST as then it won't run on unsupported platforms. The test itself must also be wrapped in a #if wxUSE_UIACTIONSIMULATOR block. e) There are a number of classes that are available to help with testing GUI elements. Firstly throughout the test run there is a frame of type wxTestableFrame that you can access through `wxTheApp->GetTopWindow()`. This class adds two new functions, GetEventCount, which takes an optional wxEventType. It then returns the number of events of that type that it has received since the last call. Passing nothing returns the total number of event received since the last call. Also there is OnEvent, which counts the events based on type that are passed to it. To make it easy to count events there is also a new class called EventCounter which takes a window and event type and connects the window to the top level wxTestableFrame with the specific event type. It disconnects again once it is out of scope. It simply reduces the amount of typing required to count events. 3. add a `` tag for your source file to tests/test.bkl. Make sure it's in the correct section: the one starting `