複製鏈接
請複製以下鏈接發送給好友

CppUnit

鎖定
CppUnit測試是軟件開發過程中必不可少的一個部分,是軟件質量的保障的重要手段. 單元測試作為代碼級最小的單位的測試,在軟件開發過程中起着舉足輕重的作用。
中文名
CppUnit
外文名
CppUnit
開發環境
Windows
測    試
單元測試
移    植
JUnit
框    架
GNULGPL條約

CppUnit編程信息

極限編程(XP)推崇測試優先原則,由此引發了軟件開發方法從傳統的瀑布模型轉向以測試為驅動的敏捷開發模式的革命。在這場軟件開發方法革命中,以xUnit系列的單元測試框架是一切的中心。xUnit的成員有很多,如JUnitNUnit.PythonUnit,HtmlUnit,HttpUnit等。CppUnit就是xUnit家族中的一員,它是一個專門面向C++的單元測試框架。
CppUnit是Micheal Feathers由JUnit移植過來的一個在GNU LGPL條約下的並在sourcefogre網站上開源的C++單元測試框架。

CppUnit安裝

Windows
(選擇開發環境為MS Visual C++ 6.0)需要如下五個步驟:
一 、到CppUnit 的官方網站上下載CppUnit的軟件包。
二、 編譯、安裝CppUnit庫。在VC中打開CPPUNITHOME/src/CppUnitLibraries.dsw,選擇“Build | BatchBuild...”,選中所有的項目,點擊build按鈕。在CPPUNITHOME/lib/下生成所需要的所有庫文件。*CPPUNITHOME是CppUnit在你磁盤上的目錄。下同。
三、在Visual C++中進行設置。告訴VC在哪裏能找到CppUnit中的程序文件和庫文件:打開“Tools | Options...”,切換到'Directories'標籤頁,選擇'include files',添加CPPUNITHOME/include/;切換到'libraries files'標籤頁,添加CPPUNITHOME/lib/;切換到'source files'標籤頁,添加CPPUNITHOME/src/cppunit/,保存。
四、在測試代碼中進行設置。在VC中打開你寫的測試程序,啓動Project Settings對話框,切換到'C++'標籤頁,選擇'Code generation'項,對於release版,選擇'Multithreaded DLL',對於Debug版,選擇'Debug Multithreaded DLL'。同樣是在這個標籤頁,選擇'C++ langage'項,選擇All Configurations,選擇'enable Run-Time Type Information (RTTI)'。切換到'Link'標籤頁,在'Object/library modules'中添入需要的lib文件cppunitX.lib (debug模式為cppunitd.lib, release 模式為cppunit.lib )和testrunnerX.lib(debug模式為testrunnerd.lib, release 模式testrunner.lib,debug Unicode模式為testrunnerud.lib, release Unicode模式為testrunneru.lib)
五、添加系統路徑。為使測試程序在運行時能找到CppUnit提供的dll,我們在環境變量中指出CppUnit提供的dll的路徑:在我的電腦中,打開環境變量,編輯系統變量中的path變量,向其中添加CPPUNITHOME\lib,從新啓動計算機,使設置生效。
Linux
  1. Ubuntu或者Debian下的安裝:
sudo apt-get install libcppunit-dev libcppunit-doc
2. 源代碼安裝
一、下載最新的CppUnit的源碼包,並解壓: tar -zxvf cppunit1.21.1.tar.gz
二、進入CppUnit介質的目錄,依次執行:
cd cppunit1.21.1
./configure
make
sudo make install(需要root權限將庫文件拷貝到/usr/local/lib目錄下)
如果編譯安裝時遇到錯誤可以參考資料2 [1] 
三、將CppUnit生成的動態庫文件所在的路徑(默認是:/usr/local/lib)添加到/etc/ld.so.conf文件裏,然後運行ldconfig。注意:可以通過 cat /etc/ld.so.conf.d/* 觀察一下/usr/local/lib是否已經在動態連接庫路徑中,然後在做修改。
如果在命令行(cmd DOS/Shell)下,在編譯連接程序時,需要使用-lcppunit 選項,如g++ -lcppunit 1.cpp 2.cpp 3.cpp。如果是在VC 6.0 的IDE中,需要配置一下連接路徑,將-lcppunit添加到連接路徑中。

CppUnit簡介

CppUnit按照層次來管理測試,最底層的就是TestCase,當有了幾個TestCase以後,可以將它們組織成TestFixture。在TestFixture中,可以建立被測試的類的實例,並編寫TestCase對類實例進行測試,多個TestFixture可以通過TestSuite來對測試進行管理。 [1] 
通過派生TestFixture類來設計某個類或某組相關功能的單元測試,Fixture定義公共函數setUp()初始化每個成員變量,tearDown()來釋放setUp中使用的資源。在每個測試中,CPPUNIT_ASSERT(bool)來判斷某個函數和表達式的正確性,在派生類的聲明中,通過CPPUNIT_TEST來增加對應的測試函數,通過CPPUNIT_TEST_SUITE和CPPUNIT_TEST_SUITE_END來分裝所有的測試函數,規定這些測試函數執行的順序. [1] 
框架
(1)CppUnit核心部分(core)
  • 基本測試類:Test,TestFixture,TestCase,TestSuite
  • 測試結果記錄:SychronizedObject,TestListener,TestResult
  • 錯誤處理:TestFailure,SourceLine,Execption,NotEqualException
  • 斷言:Asserter,TestAssert
(2)輸出部分(Ouput)
  • 基本部件:Outputter,TestResultCollector
  • 衍生類:TestOutputter,CompilerOutputer,XmlOutputer
(3)輔助部分(Helper)
TypeInfoHelper,TestFactory,TestFactoryRegistry,NamedRegisters,TestSuiteFactory,
TesSuiteBuilder,TestCaller,AutoRegisterSuite,HelperMacros.
(4)擴展部分(Extension)
TestDecorator,RepeatedTest,Orthodox,TestSetUp
(5)監聽者部分(Listener)
TestSucessListener,TextTestProgressListener,TextTestResult
(6)界面部分(UI)
TestRunner(TextUI,MfcUI,QtUI)
(7)移植(Portabilty):OStringStream

CppUnit使用

一個CppUnit版的Helloworld:
/*Program:testcppunit.cpp -- a simple hellow example which use the cppunit tool*/
#include <iostream>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class Test : public CPPUNIT_NS::TestCase
{
    CPPUNIT_TEST_SUITE(Test);
    CPPUNIT_TEST(testHelloWorld);
    CPPUNIT_TEST_SUITE_END();
    public:
        void setUp(
void) {}
        void tearDown(void) {}
    protected:
        void testHelloWorld(void) { std::cout << "Hello, world!" << std::
endl; }
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
int main( int argc, char **argv )
{
    // Create the event manager and test controller
    CPPUNIT_NS::TestResult controller;
    // Add a listener that colllects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener( &result );       
    // Add a listener that print dots as test run.
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener( &progress );     

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
    runner.run( controller );
    return result.wasSuccessful() ? 0 : 1;
}
編譯命令: gcc -o testcppunit testcppunit.cpp -lcppunit (如果是VC/windows的話,使用cl 代替gcc--注意要配置cl的環境變量才可以在cmd命令行下使用。當然,也可以使用VC6.0的集成環境來測試.)
運行結果:
Test::testHelloWorldHello,world!
:OK

CppUnit深入學習

介紹一些擴展閲讀資料:
  1. CppUnit 快速使用指南 [2] 
  2. CppUnit源代碼解讀
參考資料