Unit Test Examples
This appendix contains an example of a
unit test header file listing and a
unit test source file listing as well
as a
unit test container source file listing.
Unit Test Header File Example
/***************************************************************************
mymoneyexceptiontest.h
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef __MYMONEYEXCEPTIONTEST_H__
#define __MYMONEYEXCEPTIONTEST_H__
#include <cppunit/extensions/HelperMacros.h>
#define private public
#include "mymoneyutils.h"
#include "mymoneyexception.h"
#undef private
class MyMoneyExceptionTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MyMoneyExceptionTest);
CPPUNIT_TEST(testDefaultConstructor);
CPPUNIT_TEST(testConstructor);
CPPUNIT_TEST_SUITE_END();
protected:
public:
MyMoneyExceptionTest();
void setUp();
void tearDown();
void testDefaultConstructor();
void testConstructor();
};
#endif
Unit Test Source File Example
/***************************************************************************
mymoneyexceptiontest.cpp
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "mymoneyexceptiontest.h"
MyMoneyExceptionTest::MyMoneyExceptionTest()
{
}
void MyMoneyExceptionTest::setUp()
{
}
void MyMoneyExceptionTest::tearDown()
{
}
void MyMoneyExceptionTest::testDefaultConstructor()
{
MyMoneyException *e = new MYMONEYEXCEPTION("Message");
CPPUNIT_ASSERT(e->what() == "Message");
CPPUNIT_ASSERT(e->line() == __LINE__-2);
CPPUNIT_ASSERT(e->file() == __FILE__);
delete e;
}
void MyMoneyExceptionTest::testConstructor()
{
MyMoneyException *e = new MyMoneyException("New message",
"Joe's file", 1234);
CPPUNIT_ASSERT(e->what() == "New message");
CPPUNIT_ASSERT(e->line() == 1234);
CPPUNIT_ASSERT(e->file() == "Joe's file");
delete e;
}
Unit Test Container Source File Example
This test environment also contains a reference to a memory usage checker
which can safely be ignored. It is also contained in the &app; environment
and is a great help if looking for memory leaks. Also notice the usage of
the C++ preprocessor directive #ifdef HAVE_LIBCPPUNIT
to avoid compile errors when CPPUNIT is not installed.
Another specialty that is not required by CPPUNIT is the specific
TestProgressListener. It is used here to print the name of the fixture that
is currently running. Since this method is called upon start of each
test case, some logic is necessary to print the name only once.
/***************************************************************************
autotest.cpp
-------------------
copyright : (C) 2002 by Thomas Baumgart
email : ipwizard@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <iostream>
#ifdef HAVE_LIBCPPUNIT
#include "cppunit/TextTestRunner.h"
#include "cppunit/TextTestResult.h"
#include "cppunit/TestSuite.h"
#include "cppunit/extensions/HelperMacros.h"
#include "mymoneyexceptiontest.h"
#include "cppunit/TextTestProgressListener.h"
class MyProgressListener : public CppUnit::TextTestProgressListener
{
void startTest(CppUnit::Test *test) {
QString name = test->getName().c_str();
name = name.mid(2); // cut off first 2 chars
name = name.left(name.find('.'));
if(m_name != name) {
if(m_name != "")
cout << endl;
cout << "Running: " << name << endl;
m_name = name;
}
}
private:
QString m_name;
};
#endif
int
main(int argc, char** argv)
{
#ifdef HAVE_LIBCPPUNIT
#ifdef _CHECK_MEMORY
_CheckMemory_Init(0);
#endif
CPPUNIT_TEST_SUITE_REGISTRATION(MyMoneyExceptionTest);
CppUnit::TestFactoryRegistry ®istry =
CppUnit::TestFactoryRegistry::getRegistry();
CppUnit::Test *suite = registry.makeTest();
CppUnit::TextTestRunner* runner = new CppUnit::TextTestRunner();
runner->addTest(suite);
MyProgressListener progress;
runner->eventManager().addListener(&progress);
runner->run();
delete runner;
#ifdef _CHECK_MEMORY
chkmem.CheckMemoryLeak( true );
_CheckMemory_End();
#endif // _CHECK_MEMORY
#else
std::cout << "libcppunit not installed. no automatic tests available."
<< std::endl;
#endif // HAVE_LIBCPPUNIT
return 0;
}