CppUnit project page | FAQ |
The example explored in this article can be found in examples/Money/
.
In the following document, $CPPUNIT is the directory where you unpacked CppUnit: $CPPUNIT/: include/ lib/ src/ cppunit/
First, you need to compile CppUnit libraries:
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/.
Creates a new console application ('a simple application' template will do). Let's link CppUnit library to our project. In the project settings:
We're done !
We'll use autoconf
and automake
to make it simple to create our build environment. Create a directory somewhere to hold the code we're going to build. Create configure.in
and Makefile.am
in that directory to get started.
configure.in
dnl Process this file with autoconf to produce a configure script. AC_INIT(Makefile.am) AM_INIT_AUTOMAKE(money,0.1) AM_PATH_CPPUNIT(1.9.6) AC_PROG_CXX AC_PROG_CC AC_PROG_INSTALL AC_OUTPUT(Makefile)
Makefile.am
# 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_LDFLAGS = $(CPPUNIT_LIBS) -ldl
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:
MoneyApp.cpp
VC++: Compile and run (Ctrl+F5).
Unix: First build. Since we don't have all the file yet, let's create them and build our application for the first time:
touch Money.h MoneyTest.h MoneyTest.cpp aclocal -I /usr/local/share/aclocal autoconf automake -a touch NEWS README AUTHORS ChangeLog # To make automake happy ./configure make check
Our application will report that everything is fine and no test were run. So let's add some tests...
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:
$(TargetPath)
$(TargetPath)
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 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?)
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:
MoneyTest.h:
MoneyTest.cpp
Compile. As expected, it reports that a test failed. Press the F4
key (Go to next Error). VC++ jump right to our failed assertion CPPUNIT_FAIL. We can not ask better in term of integration!
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)
Well, we have everything set up, let's start doing some real testing.
Let's write our first real test. A test is usually decomposed in three parts:
checking the result of the processing
Well, we finally have a good start of what our Money class will look like. Let's start implementing...
Money.h
Include Money.h
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:
Money.h
Compile. Our test finally pass! Let's add some functionnality to our Money class.
We want to check if to Money object are equal. Let's start by adding a new test to the suite, then add our method:
MoneyTest.h
MoneyTest.cpp
Let's implements operator
==
and operator
!=
in Money.h:
Money.h
Compile, run... Ooops... Press F4, it seems we're having trouble with operator
!=
. Let's fix that:
Compile, run. Finally got it working!
Let's add our test 'testAdd' to MoneyTest. You know the routine...
MoneyTest.cpp
While writing that test case, you ask yourself, what is the result of adding money of currencies. Obviously this is an error and it should be reported, say let throw an exception, say IncompatibleMoneyError
, when the currencies are not equal. We will write another test case for this later. For now let get our testAdd() case working:
Money.h
Compile, run. Miracle, everything is fine! Just to be sure the test is indeed working, in the above code, change m_amount
+=
to -=
. Build and check that it fails (always be suspicious of test that work the first time: you may have forgotten to add it to the suite for example)! Change the code back so that all the tests are working.
Let's the incompatible money test case before we forget about it... That test case expect an IncompatibleMoneyError
exception to be thrown. CppUnit can test that for us, you need to specify that the test case expect an exception when you add it to the suite:
MoneyTest.h
By convention, you end the name of such tests with 'Throw'
, that way, you know that the test expect an exception to be thrown. Let's write our test case:
MoneyTest.cpp
Compile... Ooops, forgot to declare the exception class. Let's do that:
Money.h
Compile. As expected testAddThrow() fail... Let's fix that:
Money.h
Compile. Our test finaly passes!
TODO:
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 ;-)
Send comments to: CppUnit Developers |