1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*! \page money_example Money, a step by step example
\section Table of contents
- \ref sec_setting_vc
- \ref sec_setting_unix
- \ref sec_running_test
- \ref sec_adding_testfixture
- \ref sec_first_tests
- \ref sec_more_tests
- \ref sec_credits
The example explored in this article can be found in \c examples/Money/.
\section sec_setting_vc Setting up your project (VC++)
\subsection sec_install Compiling and installing CppUnit libaries
In the following document, $CPPUNIT is the directory where you unpacked %CppUnit:
~~~~~
$CPPUNIT/:
include/
lib/
src/
cppunit/
~~~~~
First, you need to compile %CppUnit libraries:
- Open the `$CPPUNIT/src/CppUnitLibrariesXXXX.sln` workspace in VC++.
- In the 'Build' menu, select 'Batch Build...'
- In the batch build dialog, select all projects and press the build button.
- The resulting libraries can be found in the `$CPPUNIT/lib/` directory.
Once it is done, you need to tell VC++ where are the includes and libraries
to use them in other projects. Open the 'Tools/Options...' dialog, and in the
'Directories' tab, select 'include files' in the combo. Add a new entry that
points to `$CPPUNIT/include/`. Change to 'libraries files' in the combo and
add a new entry for `$CPPUNIT/lib/`. Repeat the process with 'source files'
and add `$CPPUNIT/src/cppunit/`.
\subsection sec_getting_started Getting started
Creates a new console application ('a simple application' template will do).
Let's link %CppUnit library to our project. In the project settings:
- In tab 'C++', combo 'Code generation', set the combo to 'Multithreaded DLL'
for the release configuration, and 'Debug Multithreaded DLL' for the debug
configure,
- In tab 'C++', combo 'C++ langage', for All Configurations, check
'enable Run-Time Type Information (RTTI)',
- In tab 'Link', in the 'Object/library modules' field, add cppunitd.lib for
the debug configuration, and cppunit.lib for the release configuration.
We're done !
\section sec_setting_unix Setting up your project (Unix)
We'll use \c autoconf and \c automake to make it simple to
create our build environment. Create a directory somewhere to
hold the code we're going to build. Create \c configure.in and
\c Makefile.am in that directory to get started.
<tt>configure.ac</tt>
\verbatim
dnl Process this file with autoconf to produce a configure script.
AC_INIT([money],[0.1])
AC_CONFIG_SRCDIR(Makefile.am)
AM_INIT_AUTOMAKE
PKG_CHECK_MODULES([CPPUNIT], [cppunit >= 1.11.6])
AC_PROG_CXX
AC_PROG_CC
AC_PROG_INSTALL
AC_OUTPUT(Makefile)\endverbatim
<tt>Makefile.am</tt>
\verbatim
# Rules for the test code (use `make check` to execute)
TESTS = MoneyApp
check_PROGRAMS = $(TESTS)
MoneyApp_SOURCES = Money.h MoneyTest.h MoneyTest.cpp MoneyApp.cpp
MoneyApp_CXXFLAGS = $(CPPUNIT_CFLAGS)
MoneyApp_LDADD = $(CPPUNIT_LIBS)\endverbatim
\section sec_running_test Running our tests
We have a main that doesn't do anything. Let's start by adding the mechanics
to run our tests (remember, test before you code ;-) ). For this example,
we will use a TextTestRunner with the CompilerOutputter for post-build
testing:
<tt>MoneyApp.cpp</tt>
\code
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main()
{
// Get the top level suite from the registry
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
// Adds the test to the list of test to run
CPPUNIT_NS::TextUi::TestRunner runner;
runner.addTest( suite );
// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CPPUNIT_NS::CompilerOutputter( &runner.result(),
CPPUNIT_NS::stdCOut() ) );
// Run the test.
bool wasSucessful = runner.run();
// Return error code 1 if the one of test failed.
return wasSucessful ? 0 : 1;
}\endcode
\b VC++: Compile and run (Ctrl+F5).
\b Unix: First build. Since we don't have all the files yet, let's create them
and build our application for the first time:
\verbatim
touch Money.h MoneyTest.h MoneyTest.cpp
aclocal -I /usr/local/share/aclocal # you may need to use /usr/share/aclocal instead
autoconf
touch NEWS README AUTHORS ChangeLog # To make automake happy
automake -a
./configure
make check\endverbatim
Our application will report that everything
is fine and no tests were run. So let's add some tests...
\subsection sec_post_build Setting up automated post-build testing (VC++)
What does post-build testing means? It means that each time you compile,
the test are automatically run when the build finish. This is very
useful, if you compile often you can know that you just 'broke' something,
or that everything is still working fine.
Let's adds that to our project, In the project settings, in the
'post-build step' tab:
- Select 'All configurations' (upper left combo)
- In the 'Post-build description', enter 'Unit testing...'
- In 'post-build command(s)', add a new line: <tt>\$(TargetPath)</tt>
<tt>\$(TargetPath)</tt> expands into the name of your application:
Debug\\MoneyApp.exe in debug configuration and Release\\MoneyApp.exe in release
configuration.
What we are doing is say to VC++ to run our application for each build.
Notices the last line of \c main(), it returns a different error code,
depending on weither or not a test failed. If the code returned by
an application is not 0 in post-build step, it tell VC++ that the build
step failed.
Compile. Notice that the application's output is now in the build window.
How convenient!
(Unix: tips to integrate make check into various IDE?)
\section sec_adding_testfixture Adding the TestFixture
For this example, we are going to write a simple money class. Money
has an amount and a currency. Let's begin by creating a fixture where
we can put our tests, and add single test to test Money constructor:
<tt>MoneyTest.h:</tt>
\code
#ifndef MONEYTEST_H
#define MONEYTEST_H
#include <cppunit/extensions/HelperMacros.h>
class MoneyTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( MoneyTest );
CPPUNIT_TEST( testConstructor );
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void testConstructor();
};
#endif // MONEYTEST_H\endcode
- CPPUNIT_TEST_SUITE declares that our Fixture's test suite.
- CPPUNIT_TEST adds a test to our test suite. The test is implemented
by a method named testConstructor().
- setUp() and tearDown() are use to setUp/tearDown some fixtures. We are
not using any for now.
<tt>MoneyTest.cpp</tt>
\code
#include "MoneyTest.h"
#include "Money.h"
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );
void
MoneyTest::setUp()
{
}
void
MoneyTest::tearDown()
{
}
void
MoneyTest::testConstructor()
{
CPPUNIT_FAIL( "not implemented" );
}
\endcode
Compile. As expected, it reports that a test failed. Press the \c F4 key
(Go to next Error). VC++ jump right to our failed assertion CPPUNIT_FAIL.
We can not ask better in term of integration!
\verbatim
Compiling...
MoneyTest.cpp
Linking...
Unit testing...
.F
G:\prg\vc\Lib\cppunit\examples\money\MoneyTest.cpp(26):Assertion
Test name: MoneyTest.testConstructor
not implemented
Failures !!!
Run: 1 Failure total: 1 Failures: 1 Errors: 0
Error executing d:\winnt\system32\cmd.exe.
moneyappd.exe - 1 error(s), 0 warning(s)
\endverbatim
Well, we have everything set up, let's start doing some real testing.
\section sec_first_tests Our first tests
Let's write our first real test. A test is usually decomposed in three parts:
- setting up datas used by the test
- doing some processing based on those datas
- checking the result of the processing
\code
void
MoneyTest::testConstructor()
{
// Set up
const std::string currencyFF( "FF" );
const double longNumber = 12345678.90123;
// Process
Money money( longNumber, currencyFF );
// Check
CPPUNIT_ASSERT_EQUAL( longNumber, money.getAmount() );
CPPUNIT_ASSERT_EQUAL( currencyFF, money.getCurrency() );
}\endcode
Well, we finally have a good start of what our Money class will
look like. Let's start implementing...
<tt>Money.h</tt>
\code
#ifndef MONEY_H
#define MONEY_H
#include <string>
class Money
{
public:
Money( double amount, std::string currency )
: m_amount( amount )
, m_currency( m_currency )
{
}
double getAmount() const
{
return m_amount;
}
std::string getCurrency() const
{
return m_currency;
}
private:
double m_amount;
std::string m_currency;
};
#endif\endcode
Include <tt>Money.h</tt> in MoneyTest.cpp and compile.
Hum, an assertion failed! Press F4, and we jump to the assertion
that checks the currency of the constructed money object. The report
indicates that string is not equal to expected value. There is only
two ways for this to happen: the member was badly initialized or we
returned the wrong value. After a quick check, we find out it is the former.
Let's fix that:
<tt>Money.h</tt>
\code
Money( double amount, std::string currency )
: m_amount( amount )
, m_currency( currency )
{
}\endcode
Compile. Our test finally pass!
Let's add some functionnality to our Money class.
\section sec_more_tests Adding more tests
\subsection sec_equal Testing for equality
We want to check if two Money object are equal. Let's start by adding
a new test to the suite, then add our method:
<tt>MoneyTest.h</tt>
\code
CPPUNIT_TEST_SUITE( MoneyTest );
CPPUNIT_TEST( testConstructor );
CPPUNIT_TEST( testEqual );
CPPUNIT_TEST_SUITE_END();
public:
...
void testEqual();
\endcode
<tt>MoneyTest.cpp</tt>
\code
void
MoneyTest::testEqual()
{
// Set up
const Money money123FF( 123, "FF" );
const Money money123USD( 123, "USD" );
const Money money12FF( 12, "FF" );
const Money money12USD( 12, "USD" );
// Process & Check
CPPUNIT_ASSERT( money123FF == money123FF ); // ==
CPPUNIT_ASSERT( money12FF != money123FF ); // != amount
CPPUNIT_ASSERT( money123USD != money123FF ); // != currency
CPPUNIT_ASSERT( money12USD != money123FF ); // != currency and != amount
}\endcode
Let's implements \c operator \c == and \c operator \c != in Money.h:
<tt>Money.h</tt>
\code
class Money
{
public:
...
bool operator ==( const Money &other ) const
{
return m_amount == other.m_amount &&
m_currency == other.m_currency;
}
bool operator !=( const Money &other ) const
{
return (*this == other);
}
};
\endcode
Compile, run... Ooops... Press F4, it seems we're having trouble
with \c operator \c !=. Let's fix that:
\code
bool operator !=( const Money &other ) const
{
return !(*this == other);
}\endcode
Compile, run. Finally got it working!
\subsection sec_opadd Adding moneys
Let's add our test 'testAdd' to MoneyTest. You know the routine...
<tt>MoneyTest.h</tt>
\code
...
CPPUNIT_TEST( testAdd );
CPPUNIT_TEST_SUITE_END();
public:
...
void testAdd();
\endcode
<tt>MoneyTest.cpp</tt>
\code
void
MoneyTest::testAdd()
{
// Set up
const Money money12FF( 12, "FF" );
const Money expectedMoney( 135, "FF" );
// Process
Money money( 123, "FF" );
money += money12FF;
// Check
CPPUNIT_ASSERT( expectedMoney == money ); // add works
CPPUNIT_ASSERT( &money == &(money += money12FF) ); // add returns ref. on 'this'.
}\endcode
While writing that test case, you ask yourself, what is the result of
adding money of different currencies? Obviously this is an error and it should be
reported, for example, by throwing an exception (e.g. \c IncompatibleMoneyError)
when the currencies are not equal. We will write another test case
for this later. For now let's get our testAdd() case working:
<tt>Money.h</tt>
\code
class Money
{
public:
...
Money &operator +=( const Money &other )
{
m_amount += other.m_amount;
return *this;
}
}; \endcode
Compile, run. Miracle, everything is fine! Just to be sure the test is indeed
working, in the above code, change \c m_amount \c += to \c -=. Build and
check that it fails (always be suspicious of tests that work the first
time: you may have forgotten to add it to the suite for example)!
Change the code back so that all tests work.
Let's write the incompatible money test case before we forget about it...
That test case expects an \c IncompatibleMoneyError exception to be thrown.
You can check that with %CppUnit:
<tt>MoneyTest.h</tt>
\code
...
#include "Money.h"
...
CPPUNIT_TEST_EXCEPTION( testAddThrow, IncompatibleMoneyError );
CPPUNIT_TEST_SUITE_END();
public:
...
void testAddThrow();
};\endcode
By convention, you end the name of such tests with \c 'Throw'. That way, you
know the test expects an exception to be thrown. Let's write our test case:
<tt>MoneyTest.cpp</tt>
\code
void
MoneyTest::testAddThrow()
{
// Set up
const Money money123FF( 123, "FF" );
// Process
Money money( 123, "USD" );
money += money123FF; // should throw an exception
}
\endcode
Compile... Ooops, forgot to declare the exception class. Let's do that:
<tt>Money.h</tt>
\code
#include <string>
#include <stdexcept>
class IncompatibleMoneyError : public std::runtime_error
{
public:
IncompatibleMoneyError() : runtime_error( "Incompatible moneys" )
{
}
};
\endcode
Compile. As expected, testAddThrow() fails... Let's fix that:
<tt>Money.h</tt>
\code
Money &operator +=( const Money &other )
{
if ( m_currency != other.m_currency )
throw IncompatibleMoneyError();
m_amount += other.m_amount;
return *this;
}\endcode
Compile. Our test finaly passes!
TODO:
- How to use CPPUNIT_ASSERT_EQUALS with Money
- Copy constructor/Assignment operator
- Introducing fixtures
- ?
\section sec_credits Credits
This article was written by Baptiste Lepilleur. Unix configuration & set up
by Phil Verghese. Inspired from many others (JUnit, Phil's cookbook...),
and all the newbies around that keep asking me for the
'Hello world' example ;-)
*/
|