Home 

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 KMyMoney 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 &registry =
    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;
}