您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > cppUnit
linux下cppunit的安裝和使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/1/3 9:42:06 ] 推薦標(biāo)簽:cppunit 開源 源代碼

一、CppUnit linux 安裝
CppUnit測試框架的源代碼可以到 http://sourceforge.net/projects/cppunit/上下載,當(dāng)前高版本為cppunit-1.12.0.tar.gz。 上傳到linux服務(wù)器的某一個目錄下(比如事先建立的/home/cppunit目錄下)。接下來的步驟是在linux安裝cppunit(我這里的版本RedHat是內(nèi)核版本是2.4.20-8)
[root@testhost testexample]# uname -a
Linux testhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux)。
第一步:解壓文件
gzip –d  cppunit-1.12.0.tar.gz
tar –xvf  cppunit-1.12.0.tar
第二步:編譯cppunit的源代碼(在windows下我們也要編譯它)
跳轉(zhuǎn)到源文件的目錄:cd  ./ cppunit-1.12.0
生成make file文件,使用命令(需要查看configure詳細(xì)命令可以輸入info configure): ./configure (說明:$(CPPUNITSETUPDIR)表示安裝目錄(root用戶安裝缺省為/usr/local,沒有超級用戶權(quán)限的可以安裝在自己建立的目錄下))
然后安裝: make
然后檢查安裝可以跳過: make check
然后: make install (說明,make install并沒有把頭文件安裝到/usr/include目錄中,此時我們可以先把/home/cppunit/cppunit-1.12.0/include目錄下的cppunit目錄歸檔,使用命令tar -cvf cppunit.tar ./cppunit ,然后使用mv 命令把它移動到/usr/include目錄下,然后在/usr/include目錄下使用tar -xvf cppunit.tar 解規(guī)檔即可。這樣的做法有點類似于在VC環(huán)境下配置include的目錄一樣)在make check時可能會出現(xiàn)錯誤信息,可以不用管它。make install主要是將生成的靜態(tài)鏈接庫libcppunit.a和動態(tài)鏈接庫libcppunit.so拷貝到$(CPPUNITSETUPDIR)/lib目錄下,將頭文件拷貝到$(CPPUNITSETUPDIR)/include目錄下。
第三步:配置鏈接庫路徑,如果不配置路徑可能會出現(xiàn)以下類似錯誤:error while loading shared libraries: libcppunit-1.10.so.2: cannot open shared object file: No such file or directory 實際上是找不到鏈接庫,所以接下來我們使用vi 命令修改一下鏈接庫配置文件/etc/ld.so.conf文件(呵呵,順便說下,一般的配置文件都在/etc目錄下)。在其中加入一行如下:/usr/local/lib  (說明:make install時把鏈接庫文件復(fù)制到/usr/local/lib目錄下了,在windows下我們同樣需要配置庫的搜索路徑。),忘了告訴你,為了使配置文件生效,您還需要使用ldconfig命令重新裝載一下可以了。可以使用:[root@testhost lib]# ldconfig -v | grep cppunit檢查是否配置成功,到此為止,cppunit在linux下算完全安裝完成了。為了驗證是否能正常工作,下面例子可以幫助你:
二、        CppUnit linux 使用
下面的測試代碼來源于[url]www.cppunit.sourceforge.net[/url]

#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;
}

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd