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