Axel Pospischil, apos@gmx.de, october 2002
Hello to alls lowers of extreme programming and stable code,
Download this document:
CppUnit_GUI_under_VC6_0.zip
Introduction
I am a real beginner in coding C++ and XP, so don't ask me big questions.In a couple of years we can deal with that.
This document describes preparing an MS VS 6.0 C++ project to run with CppUnit.
After searching the internet several days, there is no easy step by step description on how to use the GUI of CppUnit in an project.
So i decided to make one and here it is.
Important before going on:
If you are familiar with unit testing like JUnit:Read and consider carefully ALL(!) instructions of the „INSTALL-WIN32.txt“ of the cppunit - distribution! You should plan a few hours, a pot of coffee or probably a whole day for this undertaking.If jou are NOT familiar with unit tests:
Please do yourself a favor and give yourself time to read about it. Very good information you can get on the internet - pages of "JUnit" ( search at http://www.cetus-links.org ) or extreme programming ( http://c2.com/cgi/wiki ).If you are a real freak and don't need that stuff:
I think you don't need XP, 'cause your code must be perfectly running without any bugs :)In any case:
Study carefully the ( doxygen generated - ) helpfile of CppUnit. It is not that easy to get the GUI running with an MFC program. Withour an deeply understanding of the CppUnit class hierarchie it is almost impossible. IMO thats the reason, it did not become very usual yet to use unit tests in a c++ environment.
Now have a lot of fun and effort using CppUnit :) and talk to your programming friends and tell them, that it is worth unit testing.
Axel Pospischil
apos@gmx.de
www.apos.de
worms, germany september 2002
Recources:
- german version of Visual Studio 6.0 C++ without any service pack ( never change a running system if it works fine :) )
- CppUnit 1.9.10 and its additional documentation ( search at http://sourceforge.net/ or link directly to http://sourceforge.net/projects/cppunit/ )
- netscape 7.0 & Composer ( http://www.netscape.com )
Preparation
Put cppunit/lib/testrunner.dll an testrunnerd.dll into the /debug folder.
If you don't do this, the testrunner won't start!
1. Changing the project-settings ( c++ & link -tab)
1.1 Fill in the right include-path for the preprocessor tab
1.2. Check the RTTI at c++ tab
1.3. Link to the appropriate cppunit and testrunner libs
MyPaths ( pay attention: have a WHITESPACE between the paths !):
E:/Programmiersprachen/cppunit/cppunit/lib/cppunitd.lib E:/Programmiersprachen/cppunit/cppunit/lib/testrunnerd.lib
2. Change the visual studio options (path)
2.1 Apply the right include path to the options-dialog
MyPath: E:/Programmiersprachen/cppunit/cppunit/include
2.2. Do the same withthe lib path
MyPath: E:/Programmiersprachen/cppunit/cppunit/lib
2.3. Do the same withthe src path
MyPath: E:/Programmiersprachen/cppunit/cppunit/src/cppunit
2.4. Code Generation
2.5. There has to be a copy of "../lib/testrunnerd.dll" in the actual path
This is done eather by...- ...copying it manually to the debug path
copy path_to_cppunit/lib/testrunnerd.dll path_to_debug_of_your_program
- or letting the visual studio environment do the work for you after compilation ( don't ignore the "point" at the end of the command ! ).
copy path_to_cppunit/lib/testrunnerd.dll .
MyPath: E:/Programmiersprachen/cppunit/cppunit/lib/testrunnerd.dll
That's it for the vs 6.0 environment.
Now lets go to the program code in the testclasses and MFC...
3. Getting the testrunner-GUI working after compilation
Now go foreward and have a look in the „HostApp“ workspace. It describes how to invoke the testrunner-GUI. Add the code between „//++++“ to your MFC-Code.
_____________________________________________1. MainApp.h:
class CMainApp : public CWinApp
{
[...]
// +++++++++++++++++++++++++
private:
void RunUnitTests();
// +++++++++++++++++++++++++
}; /// END of main header file class definition
_____________________________________________
2. MainApp.cpp:
#include "stdafx.h"
#include "MainApp.h"
[...] // other includes
// +++++++++++++++++++++++++
// CppUnit: MFC TestRunner
#include <cppunit/ui/mfc/TestRunner.h>
// CppUnit: TestFactoryRegistry to retreive the top test
// suite that contains all registered tests.
#include <cppunit/extensions/TestFactoryRegistry.h>
// +++++++++++++++++++++++++
[...] // Debug #defines
// +++++++++++++++++++++++++
static AFX_EXTENSION_MODULE extTestRunner;
// +++++++++++++++++++++++++
[...] // Message-Maps and main construction of "theApp"
BOOL CMainApp::InitInstance()
{
[...] // Standard init
#ifdef _AFXDLL
Enable3dControls(); /// Diese Funktion bei Verwendung von MFC in gemeinsam genutzten DLLs aufrufen
#else
Enable3dControlsStatic(); /// Diese Funktion bei statischen MFC-Anbindungen aufrufen
#endif
// +++++++++++++++++++++++++
RunUnitTests();
// +++++++++++++++++++++++++
CMainDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
[...] // Show the main window
} // END of InitInstance()
// +++++++++++++++++++++++++
void CHostAppApp::RunUnitTests()
{
CppUnit::MfcUi::TestRunner runner;
runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
runner.run();
}
// +++++++++++++++++++++++++
4. Making a test case class
4.1. Just make an ordinary class with the class wizard and devire it from „TestCase“
You can use thef following code as a template ( just find and change „CSomeClass“ to the name of the class you would write a test to.4.1.1 Header
// CSomeClassTest.h: interface of class CSomeClassTest.
//
#ifndef _CSOMECLASSTEST_H_
#define _CSOMECLASSTEST_H_
#include <cppunit/extensions/HelperMacros.h>
/// cppunit test class, main doc text
///
/// more documentation text
class CSomeClassTest : public CppUnit::TestFixture
{
///
CPPUNIT_TEST_SUITE( CSomeClassTest );
/// place here the add_test macros
// f.e. CPPUNIT_TEST ( test_1 );
/// END test_suite macros
CPPUNIT_TEST_SUITE_END();
///
///
/// place here the protected variables for testing
protected:
/// END protected variables for testing
///
///
/// fixture
public:
void tearDown();
void setUp();
/// END public methods
///
///
/// protected test methods
protected:
// f.e. void test_1();
/// END test methods
///
};
#endif // !defined _CSOMECLASSTEST_H_
4.1.2 Implementation
// CSomeClassTest.cpp: implementation of class CSomeClassTest.
//
#include "stdafx.h"
#include "CSomeClassTest.h"
//
/// do the registry of the test_fixture
// #include <cppunit/extensions/HelperMacros.h>
CPPUNIT_TEST_SUITE_REGISTRATION( CSomeClassTest );
//
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// test fixture
//
void CSomeClassTest::setUp()
{
}
void CSomeClassTest::tearDown()
{
}
//
// test methods
//