Size: 2312
Comment: Writing a simplistic test case
|
Size: 3700
Comment: Compiling and running
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
This document describes very shortly how to install the [http://sourceforge.net/projects/cppunit/ CppUnit] framework under Unix and how to write and run a very simplistic test case. This should get you started with using Cpp``Unit, so that you can follow and try out the examples given in the [http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html CppUnit CookBook]. | This document describes very shortly how to install the [http://sourceforge.net/projects/cppunit/ CppUnit] framework under Unix and how to write and run a very simplistic test case. This should get you started with using Cpp``Unit, so that you can follow and try out the examples given in the [http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html CppUnit CookBook]. |
Line 76: | Line 76: |
= Compiling and running = Suppose we put all the above code into one file `ComplexNumberTest.cpp`. Then the following makefile will do the job for us: {{{ INCLS = -I/var/tmp/cppunit/install/include/ LIBDIRS = -L/var/tmp/cppunit/install/lib LIBS = -lcppunit -ldl # note that CppUnit needs libld.so! # Implicit rule: How to make an executable from a .cpp-file %: %.cpp ${CC} -o $@ $< ${CXXFLAGS} ${INCLS} ${LIBDIRS} ${LIBS} runtest: ComplexNumberTest export LD_LIBRARY_PATH=/var/tmp/cppunit/install/lib && ./ComplexNumberTest }}} Note that Cpp``Unit needs the library `ld.so` in the linking step. Now we can compile and run the program: {{{ $ make runtest cc -o ComplexNumberTest ComplexNumberTest.cpp -I/var/tmp/cppunit/install/include/ -L/var/tmp/cppunit/install/lib -lcppunit -ldl export LD_LIBRARY_PATH=/var/tmp/cppunit/install/lib && ./ComplexNumberTest terminate called after throwing an instance of 'CppUnit::Exception' what(): assertion failed - Expression: Complex (1, 2) + Complex(3, 4) == Complex (4, 6) /bin/sh: line 1: 1457 Aborted ./ComplexNumberTest }}} As you can see, the third assertion failed, which detects the bug in out `operator+`. Now go on and read the rest of the [http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html CppUnit CookBook] and try out the examples given there. |
This document describes very shortly how to install the [http://sourceforge.net/projects/cppunit/ CppUnit] framework under Unix and how to write and run a very simplistic test case. This should get you started with using CppUnit, so that you can follow and try out the examples given in the [http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html CppUnit CookBook].
Installing
Download the latest version of CppUnit from http://downloads.sourceforge.net/cppunit/cppunit-1.12.1.tar.gz.
Let us install it under /var/tmp/cppunit/install:
$ cd /var/tmp/ $ mv ~/cppunit-1.12.1.tar.gz . $ tar xzf cppunit-1.12.1.tar.gz $ ln -s cppunit-1.12.1 cppunit $ cd cppunit $ ./configure --prefix=`pwd`/install $ make $ make check $ make install
Writing a simplistic test case
Suppose we write a class Complex for dealing with complex numbers. In the following code, the operator+ intentionally contains a bug:
class Complex { friend bool operator==(const Complex& a, const Complex& b); friend Complex operator+(const Complex& a, const Complex& b); double real, imaginary; public: Complex( double r, double i = 0 ) : real(r), imaginary(i) {} }; bool operator==( const Complex &a, const Complex &b ) { return a.real == b.real && a.imaginary == b.imaginary; } Complex operator+( const Complex &a, const Complex &b ) { // BUG! imaginary part should be 'a.imaginary + b.imaginary' return Complex(a.real + b.real, a.imaginary + b.real); }
Now let us write a CppUnit test case for this class:
#include <cppunit/TestCase.h> class ComplexNumberTest : public CppUnit::TestCase { public: ComplexNumberTest( std::string name ) : CppUnit::TestCase( name ) {} void runTest() { CPPUNIT_ASSERT( Complex (10, 1) == Complex (10, 1) ); CPPUNIT_ASSERT( !(Complex (1, 1) == Complex (2, 2)) ); CPPUNIT_ASSERT( Complex (1, 2) + Complex(3, 4) == Complex (4, 6)); } }; int main() { ComplexNumberTest myTest("My first CppUnit test"); myTest.runTest(); }
For this, we have to subclass the TestCase class and to override the method runTest(). To check a value, we call CPPUNIT_ASSERT(bool) and pass in an expression that is true if the test succeeds.
Compiling and running
Suppose we put all the above code into one file ComplexNumberTest.cpp. Then the following makefile will do the job for us:
INCLS = -I/var/tmp/cppunit/install/include/ LIBDIRS = -L/var/tmp/cppunit/install/lib LIBS = -lcppunit -ldl # note that CppUnit needs libld.so! # Implicit rule: How to make an executable from a .cpp-file %: %.cpp ${CC} -o $@ $< ${CXXFLAGS} ${INCLS} ${LIBDIRS} ${LIBS} runtest: ComplexNumberTest export LD_LIBRARY_PATH=/var/tmp/cppunit/install/lib && ./ComplexNumberTest
Note that CppUnit needs the library ld.so in the linking step.
Now we can compile and run the program:
$ make runtest cc -o ComplexNumberTest ComplexNumberTest.cpp -I/var/tmp/cppunit/install/include/ -L/var/tmp/cppunit/install/lib -lcppunit -ldl export LD_LIBRARY_PATH=/var/tmp/cppunit/install/lib && ./ComplexNumberTest terminate called after throwing an instance of 'CppUnit::Exception' what(): assertion failed - Expression: Complex (1, 2) + Complex(3, 4) == Complex (4, 6) /bin/sh: line 1: 1457 Aborted ./ComplexNumberTest
As you can see, the third assertion failed, which detects the bug in out operator+.
Now go on and read the rest of the [http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html CppUnit CookBook] and try out the examples given there.