Differences between revisions 2 and 3
Revision 2 as of 2008-12-15 13:42:57
Size: 779
Editor: mpiat1403
Comment: Added links
Revision 3 as of 2008-12-15 13:50:08
Size: 2312
Editor: mpiat1403
Comment: Writing a simplistic test case
Deletions are marked like this. Additions are marked like this.
Line 21: Line 21:


= 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 Cpp``Unit 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.

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.

CompleteSearch: completesearch/InstallingAndRunningCppUnit (last edited 2008-12-17 12:00:29 by mpiat1403)