comparison ext/UnitTest++/docs/UnitTest++.html @ 37:0d325e9d5953

added unittest++ files into ext. Not hooked into build scripts yet
author jasoka@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 12 Jul 2008 12:00:57 +0000
parents
children
comparison
equal deleted inserted replaced
36:6f1227f4932b 37:0d325e9d5953
1 <html>
2 <head>
3 <title>UnitTest++ in brief</title>
4 </head>
5 <body>
6 <h1>UnitTest++ in brief</h1>
7 <h2>Introduction</h2>
8 <p>This little document serves as bare-bones documentation for UnitTest++.</p>
9
10 <p>For background, goals and license details, see:</p>
11
12 <ul>
13 <li><a href="http://unittest-cpp.sourceforge.net/">The UnitTest++ home page</a></li>
14 <li><a href="http://www.gamesfromwithin.com/articles/0603/000108.html">Noel Llopis' announcement</a></li>
15 </ul>
16
17 <p>The documentation, while sparse, aims to be practical, so it should give you enough info to get started using UnitTest++ as fast as possible.</p>
18
19 <h2>Building UnitTest++</h2>
20 <p>Building UnitTest++ will be specific to each platform and build environment, but it should be straightforward.</p>
21
22 <h3>Building with Visual Studio</h3>
23 <p>If you are using Visual Studio, go for either of the provided .sln files, depending on version. There are no prefabricated solutions for versions earlier than VS.NET 2003, but we have had reports of people building UnitTest++ with at least VS.NET 2002.</p>
24
25 <h3>Building with Make</h3>
26 <p>The bundled makefile is written to build with g++. It also needs <code>sed</code> installed in the path, and to be able to use the <code>mv</code> and <code>rm</code> shell commands. The makefile should be usable on most Posix-like platforms.</p>
27
28 <p>Do "make all" to generate a library and test executable. A final build step runs all unit tests to make sure that the result works as expected.</p>
29
30 <h3>Packaging</h3>
31 <p>You'll probably want to keep the generated library in a shared space in source control, so you can reuse it for multiple test projects. A redistributable package of UnitTest++ would consist of the generated library file, and all of the header files in <code>UnitTest++/src/</code> and its per-platform subfolders. The <code>tests</code> directory only contains the unit tests for the library, and need not be included.</p>
32
33 <h2>Using UnitTest++</h2>
34 <p>The source code for UnitTest++ comes with a full test suite written <em>using</em> UnitTest++. This is a great place to learn techniques for testing. There is one sample .cpp file: <code>UnitTest++/src/tests/TestUnitTest++.cpp</code>. It covers most of UnitTest++'s features in an easy-to-grasp context, so start there if you want a quick overview of typical usage.</p>
35
36 <h3>Getting started</h3>
37 <p>Listed below is a minimal C++ program to run a failing test through UnitTest++.</p>
38
39 <pre>
40 // test.cpp
41 #include &lt;UnitTest++.h&gt;
42
43 TEST(FailSpectacularly)
44 {
45 CHECK(false);
46 }
47
48 int main()
49 {
50 return UnitTest::RunAllTests();
51 }
52 </pre>
53
54 <p><code>UnitTest++.h</code> is a facade header for UnitTest++, so including that should get you all features of the library. All classes and free functions are placed in namespace <code>UnitTest</code>, so you need to either qualify their full names (as with <code>RunAllTests()</code> in the example) or add a <code>using namespace UnitTest;</code> statement in your .cpp files. Note that any mention of UnitTest++ functions and classes in this document assume that the <code>UnitTest</code> namespace has been opened.</p>
55
56 <p>Compiling and linking this program with UnitTest++'s static library into an executable, and running it, will produce the following output (details may vary):</p>
57
58 <pre>
59 .\test.cpp(5): error: Failure in FailSpectacularly: false
60 FAILED: 1 out of 1 tests failed (1 failures).
61 Test time: 0.00 seconds.
62 </pre>
63
64 <p>UnitTest++ attempts to report every failure in an IDE-friendly format, depending on platform (e.g. you can double-click it in Visual Studio's error list.) The exit code will be the number of failed tests, so that a failed test run always returns a non-zero exit code.</p>
65
66 <h3>Test macros</h3>
67 <p>To add a test, simply put the following code in a .cpp file of your choice:</p>
68
69 <pre>
70 TEST(YourTestName)
71 {
72 }
73 </pre>
74
75 <p>The <code>TEST</code> macro contains enough machinery to turn this slightly odd-looking syntax into legal C++, and automatically register the test in a global list. This test list forms the basis of what is executed by <code>RunAllTests()</code>.</p>
76
77 <p>If you want to re-use a set of test data for more than one test, or provide setup/teardown for tests, you can use the <code>TEST_FIXTURE</code> macro instead. The macro requires that you pass it a class name that it will instantiate, so any setup and teardown code should be in its constructor and destructor.</p>
78
79 <pre>
80 struct SomeFixture
81 {
82 SomeFixture() { /* some setup */ }
83 ~SomeFixture() { /* some teardown */ }
84
85 int testData;
86 };
87
88 TEST_FIXTURE(SomeFixture, YourTestName)
89 {
90 int temp = testData;
91 }
92 </pre>
93
94 <p>Note how members of the fixture are used as if they are a part of the test, since the macro-generated test class derives from the provided fixture class.</p>
95
96 <h3>Suite macros</h3>
97 <p>Tests can be grouped into suites, using the <code>SUITE</code> macro. A suite serves as a namespace for test names, so that the same test name can be used in two difference contexts.</p>
98
99 <pre>
100 SUITE(YourSuiteName)
101 {
102 TEST(YourTestName)
103 {
104 }
105
106 TEST(YourOtherTestName)
107 {
108 }
109 }
110 </pre>
111
112 <p>This will place the tests into a C++ namespace called <code>YourSuiteName</code>, and make the suite name available to UnitTest++. <code>RunAllTests()</code> can be called for a specific suite name, so you can use this to build named groups of tests to be run together.</p>
113
114 <h3>Simple check macros</h3>
115 <p>In test cases, we want to check the results of our system under test. UnitTest++ provides a number of check macros that handle comparison and proper failure reporting.</p>
116
117 <p>The most basic variety is the boolean <code>CHECK</code> macro:</p>
118
119 <pre>
120 CHECK(false); // fails
121 </pre>
122
123 <p>It will fail if the boolean expression evaluates to false.</p>
124
125 <p>For equality checks, it's generally better to use <code>CHECK_EQUAL</code>:</p>
126
127 <pre>
128 CHECK_EQUAL(10, 20); // fails
129 CHECK_EQUAL("foo", "bar"); // fails
130 </pre>
131
132 <p>Note how <code>CHECK_EQUAL</code> is overloaded for C strings, so you don't have to resort to <code>strcmp</code> or similar. There is no facility for case-insensitive comparison or string searches, so you may have to drop down to a plain boolean <code>CHECK</code> with help from the CRT:</p>
133
134 <pre>
135 CHECK(std::strstr("zaza", "az") != 0); // succeeds
136 </pre>
137
138 <p>For floating-point comparison, equality <a href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">isn't necessarily well-defined</a>, so you should prefer the <code>CHECK_CLOSE</code> macro:</p>
139
140 <pre>
141 CHECK_CLOSE(3.14, 3.1415, 0.01); // succeeds
142 </pre>
143
144 <p>All of the macros are tailored to avoid unintended side-effects, for example:</p>
145
146 <pre>
147 TEST(CheckMacrosHaveNoSideEffects)
148 {
149 int i = 4;
150 CHECK_EQUAL(5, ++i); // succeeds
151 CHECK_EQUAL(5, i); // succeeds
152 }
153 </pre>
154
155 <p>The check macros guarantee that the <code>++i</code> expression isn't repeated internally, as demonstrated above.</p>
156
157 <h3>Array check macros</h3>
158 <p>There is a set of check macros for array comparison as well:</p>
159
160 <pre>
161 const float oned[2] = { 10, 20 };
162 CHECK_ARRAY_EQUAL(oned, oned, 2); // succeeds
163 CHECK_ARRAY_CLOSE(oned, oned, 2, 0.00); // succeeds
164
165 const float twod[2][3] = { {0, 1, 2}, {2, 3, 4} };
166 CHECK_ARRAY2D_CLOSE(twod, twod, 2, 3, 0.00); // succeeds
167 </pre>
168
169 <p>The array equal macro compares elements using <code>operator==</code>, so <code>CHECK_ARRAY_EQUAL</code> won't work for an array of C strings, for example.</p>
170
171 <p>The array close macros are similar to the regular CHECK_CLOSE macro, and are really only useful for scalar types, that can be compared in terms of a difference between two array elements.</p>
172
173 <p>Note that the one-dimensional array macros work for <code>std::vector</code> as well, as it can be indexed just as a C array.</p>
174
175 <h3>Exception check macros</h3>
176 <p>Finally, there's a <code>CHECK_THROW</code> macro, which asserts that its enclosed expression throws the specified type:</p>
177
178 <pre>
179 struct TestException {};
180 CHECK_THROW(throw TestException(), TestException); // succeeds
181 </pre>
182
183 <p>UnitTest++ natively catches exceptions if your test code doesn't. So if your code under test throws any exception UnitTest++ will fail the test and report either using the <code>what()</code> method for <code>std::exception</code> derivatives or just a plain message for unknown exception types.</p>
184
185 <p>Should your test or code raise an irrecoverable error (an Access Violation on Win32, for example, or a signal on Linux), UnitTest++ will attempt to map them to an exception and fail the test, just as for other unhandled exceptions.</p>
186
187 <h3>Time constraints</h3>
188 <p>UnitTest++ can fail a test if it takes too long to complete, using so-called time constraints.</p>
189
190 <p>They come in two flavors; <em>local</em> and <em>global</em> time constraints.</p>
191
192 <p>Local time constraints are limited to the current scope, like so:</p>
193
194 <pre>
195 TEST(YourTimedTest)
196 {
197 // Lengthy setup...
198
199 {
200 UNITTEST_TIME_CONSTRAINT(50);
201
202 // Do time-critical stuff
203 }
204
205 // Lengthy teardown...
206 }
207 </pre>
208
209 <p>The test will fail if the "Do time-critical stuff" block takes longer than 50 ms to complete. The time-consuming setup and teardown are not measured, since the time constraint is scope-bound. It's perfectly valid to have multiple local time constraints in the same test, as long as there is only one per block.</p>
210
211 <p>A global time constraint, on the other hand, requires that all of the tests in a test run are faster than a specified amount of time. This allows you, when you run a suite of tests, to ask UnitTest++ to fail it entirely if any test exceeds the global constraint. The max time is passed as a parameter to an overload of <code>RunAllTests()</code>.</p>
212
213 <p>If you want to use a global time constraint, but have one test that is notoriously slow, you can exempt it from inspection by using the <code>UNITTEST_TIME_CONSTRAINT_EXEMPT</code> macro anywhere inside the test body.</p>
214
215 <pre>
216 TEST(NotoriouslySlowTest)
217 {
218 UNITTEST_TIME_CONSTRAINT_EXEMPT();
219
220 // Oh boy, this is going to take a while
221 ...
222 }
223 </pre>
224
225 <h3>Test runners</h3>
226 <p>The <code>RunAllTests()</code> function has an overload that lets you customize the behavior of the runner, such as global time constraints, custom reporters, which suite to run, etc.</p>
227
228 <pre>
229 int RunAllTests(TestReporter& reporter, TestList const& list, char const* suiteName, int const maxTestTimeInMs);
230 </pre>
231
232 <p>If you attempt to pass custom parameters to <code>RunAllTests()</code>, note that the <code>list</code> parameter should have the value <code>Test::GetTestList()</code>.</p>
233
234 <p>The parameterless <code>RunAllTests()</code> is a simple wrapper for this one, with sensible defaults.</p>
235
236 <h3>Example setup</h3>
237 <p>How to create a new test project varies depending on your environment, but here are some directions on common file structure and usage.</p>
238
239 <p>The general idea is that you keep one <code>Main.cpp</code> file with the entry-point which calls <code>RunAllTests()</code>.</p>
240
241 <p>Then you can simply compile and link new .cpp files at will, typically one per test suite.</p>
242
243 <pre>
244 + ShaverTests/
245 |
246 +- Main.cpp
247 |
248 +- TestBrush.cpp
249 +- TestEngine.cpp
250 +- TestRazor.cpp
251 </pre>
252
253 <p>Each of the <code>Test*.cpp</code> files will contain one or more <code>TEST</code> macro incantations with the associated test code. There are no source-level dependencies between <code>Main.cpp</code> and <code>Test*.cpp</code>, as the <code>TEST</code> macro handles the registration and setup necessary for <code>RunAllTests()</code> to find all tests compiled into the same final executable.</p>
254
255 <p>UnitTest++ does not require this structure, even if this is how the library itself does it. As long as your test project contains one or more <code>TESTs</code> and calls <code>RunAllTests()</code> at one point or another, it will be handled by UnitTest++.</p>
256
257 <p>It's common to make the generated executable start as a post-build step, so that merely building your test project will run the tests as well. Since the exit code is the count of failures, a failed test will generally break the build, as most build engines will fail a build if any step returns a non-zero exit code.</p>
258
259 </body>
260 </html>