Checklist CppUnit and VC 6.0

 

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:





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

preprocessor_tab

1.2. Check the RTTI  at c++ tab

cpp_tab

1.3. Link to the appropriate cppunit and testrunner libs

link_tab

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

paths_include

MyPath: E:/Programmiersprachen/cppunit/cppunit/include

2.2. Do the same withthe lib path


paths_lib

MyPath: E:/Programmiersprachen/cppunit/cppunit/lib

2.3. Do the same withthe src path

paths_src

MyPath: E:/Programmiersprachen/cppunit/cppunit/src/cppunit

 

2.4. Code Generation
code_genaration

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 .

copy_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
//





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值