X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4f09729dfb63907ee07fc9395ff47f6411eb7469..7a36d9c7c1a077ba7e05c248bcf19c4c9bb1356f:/docs/tech/tn0017.txt diff --git a/docs/tech/tn0017.txt b/docs/tech/tn0017.txt index e6aa48491c..eeff4c2fee 100644 --- a/docs/tech/tn0017.txt +++ b/docs/tech/tn0017.txt @@ -1,23 +1,23 @@ - How to write unit tests for wxWindows + How to write unit tests for wxWidgets ===================================== - Unit tests for wxWindows are written using small cppunit framework. To compile +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 exlpains how to do it while the second one explains how to write the +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.8.0) + (latest version as of the time of this writing is 1.10.2) 2. Build the library: - a) Under Windows using VC++ (both versions 6 and 7 work): + a) Under Windows using VC++ (versions 6, 7, 8 & 9 work): - 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 IDEA + using "Tools|Options" menu in VC IDE b) Under Unix: run configure && make && make install as usual @@ -27,17 +27,119 @@ II. Writing tests with CppUnit 1. Create a new directory tests/foo -2. Write the main.cpp file for the test program copying, if you want, +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: - Read CppUnit documentation for more. + // 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 '