在VC下使用CppUnit做單元測試的簡單步驟

發表于:2009-10-10來源:作者:點擊數: 標簽:
1.取得Cppunit發行版本,下載的是 cppunit -1.10.2 2.使用INSTALL-WIN32.txt, 3.查看examples中的例子,觀看其配置。 Libraries: ---------- All the compiled libraries and DLL can be found in the 'lib' directory. Most libraries can be build from src
 1.取得Cppunit發行版本,下載的是cppunit-1.10.2
    2.使用INSTALL-WIN32.txt,
    3.查看examples中的例子,觀看其配置。

    Libraries:
    ----------

    All the compiled libraries and DLL can be found in the 'lib' directory.
    Most libraries can be build from src/CppUnitLibraries.dsw workspace.

    lib\:
    cppunit.lib    : CppUnit static library "Multithreaded DLL"
    cppunitd.lib   : CppUnit static library "Debug Multithreaded DLL"
    cppunit_dll.dll   : CppUnit dynamic library (DLL) "Multithreaded DLL"
    cppunit_dll.lib   : CppUnit dynamic import library "Multithreaded DLL"
    cppunitd_dll.dll  : CppUnit dynamic library (DLL) "Debug Multithreaded DLL"
    cppunitd_dll.lib  : CppUnit dynamic import library "Debug Multithreaded DLL"
    qttestrunner.dll  : QT TestRunner dynamic library (DLL) "Multithreaded DLL"
    qttestrunner.lib  : QT TestRunner import library "Multithreaded DLL"
    testrunner.dll   : MFC TestRunner dynamic library (DLL) "Multithreaded DLL"
    testrunner.lib   : MFC TestRunner import library "Multithreaded DLL"
    testrunnerd.dll   : MFC TestRunner dynamic library (DLL) "Debug Multithreaded DLL"
    testrunnerd.lib   : MFC TestRunner import library "Debug Multithreaded DLL"
    testrunneru.dll   : MFC Unicode TestRunner dynamic library (DLL) "Multithreaded DLL"
    testrunneru.lib   : MFC Unicode TestRunner import library "Multithreaded DLL"
    testrunnerud.dll  : MFC Unicode TestRunner dynamic library (DLL) "Debug Multithreaded DLL"
    testrunnerud.lib  : MFC Unicode TestRunner import library "Debug Multithreaded DLL"
    TestRunnerDSPlugIn.dll : The add-in you register in VC++.


    A. 新建一個MFC應用程序

    B. 在“工具”-選項-目錄

                            
    C. 在工程配置里面選擇RTTI

                           

    Link下加入 Debug\cppunitd.lib Debug\testrunnerd.lib ,記得把這輛個文件從cppunit-1.10.2\lib拷出來,把相應的dll文件也拷到    debug目錄下

     D. 在App初始化函數中App::InitInstance()的開頭加入
 #include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
在其中加入  
CppUnit::MfcUi::TestRunner runner;
runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
 runner.run();   
記得把原來的窗口注掉,不然調用的還是原來的窗口。

    E. 加入要測的類叫XXX
    我們起這個類的測試叫testXXX 類

#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>

#include "testXXX .h"


class testXXX : public CppUnit::TestCase 
{
 CPPUNIT_TEST_SUITE(testXXX );
  CPPUNIT_TEST(testcase1);   //這里就是我們的testcase的函數原型名字
  CPPUNIT_TEST(testcase2);
 CPPUNIT_TEST_SUITE_END();

public:
 void setUp();
 void tearDown();

protected:
 void testcase1();//聲明我們的測試函數
 void testcase2();

private:
 testXXX *fixture;

};

    其cpp文件必須要有

CPPUNIT_TEST_SUITE_REGISTRATION(testXXX);
    然后
void testXXX::setUp()
{
 fixture = new testXXX();//當然要按照實際的類構造你的測試對象了
}
 
void testXXX::tearDown()
{
 delete fixture;
 fixture = NULL;//析構你的測試對象
}

    下面就是你的測試函數

void testXXX::testcase1()
{
     CPPUNIT_ASSERT(condition1);//如果condition1為false激發這個assert
     CPPUNIT_ASSERT_MESSAGE(”msg“ , condition2);
    ……
}

    第二個類似這樣就可以
    運行結果類似于下面的窗口

    

    ok,好多東西我還沒有使用,今天就到這里,不過感覺挺好設置的,還是覺得比較麻煩了些,在MinGW Developer Studio不知道能否使用,我還沒有使用過,趕明兒試試。

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97