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

寫好上面的代碼后,接下來是編譯的過程,有兩種方式,一種是鏈接靜態(tài)庫(kù),一種是鏈接動(dòng)態(tài)庫(kù),下面是鏈接靜態(tài)庫(kù),編譯命令如下:
[root@testhost testexample]g++ -L /usr/local/lib/libcppunit.a hellolinux.cpp -lcppunit -ldl -o hellolinux
編譯選項(xiàng)中需要增加 -lcppunit -ldl兩個(gè)選項(xiàng),-lcppunit是連接cppunit的庫(kù),而cppunit的庫(kù)中使用了dl庫(kù)里面的函數(shù)。接下來你可以運(yùn)行一下,看看結(jié)果如何了:
[root@testhost testexample]# ./hellolinux
Test::testHelloWorldHello, world!
: OK

鏈接動(dòng)態(tài)庫(kù),編譯命令如下:
[root@testhost testexample]# g++ hellolinux.cpp -lcppunit -ldl -o hellolinux


另一例子,把如下的代碼上傳到一個(gè)目錄,如math:
  
編譯命令如下:
[root@testhost math]# g++ -L /usr/local/lib/libcppunit.a Main.cpp MathTest.cpp -lcppunit -ldl -o MathTest
然后執(zhí)行:
[root@testhost math]# ./MathTest
..F


!!!FAILURES!!!
Test Results:
Run:  2   Failures: 1   Errors: 0


1) test: MathTest::testSub (F) line: 30 MathTest.cpp
assertion failed
- Expression: result == 1

整個(gè)的源代碼如下:
/////////////////////////////////////////////////////////////////////
// Function: A simple Math test
// DATE    : 2008-1-16
// Author  : xulinlin
// Vesion  : V1.0
// FileName: MathTest.h
/////////////////////////////////////////////////////////////////////

//因?yàn)樾枰褂肨estFixture這個(gè)類,所以引入HelperMacros.h頭文件
#include "cppunit/extensions/HelperMacros.h"
//聲明一個(gè)測(cè)試類
class MathTest : public CppUnit::TestFixture {
    // 添加一個(gè)TestSuite
    CPPUNIT_TEST_SUITE( MathTest );
    // 添加測(cè)試用例到TestSuite, 定義新的測(cè)試用例需要在這兒聲明一下
    CPPUNIT_TEST( testAdd );
    CPPUNIT_TEST( testSub );
    // TestSuite添加完成
    CPPUNIT_TEST_SUITE_END();
protected:
    int x, y;      
public:
    MathTest() {}
    // 初始化函數(shù)
    void setUp ();
    // 清理函數(shù)
    void tearDown();
    // 測(cè)試加法的測(cè)試函數(shù)
    void testAdd ();
    // 測(cè)試減法的測(cè)試函數(shù)
    void testSub (); 
};

/////////////////////////////////////////////////////////////////////
// Function: A simple Math test
// DATE    : 2008-1-16
// Author  : xulinlin
// Vesion  : V1.0
// FileName: MathTest.cpp
/////////////////////////////////////////////////////////////////////
#include "MathTest.h"
// 把這個(gè)TestSuite注冊(cè)到名字為"alltest"的TestSuite中, 如果沒有定義會(huì)自動(dòng)定義
// 也可以CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );注冊(cè)到全局的一個(gè)未命名的TestSuite中.
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MathTest, "alltest" );
// 下面不變
void MathTest::setUp()
{
    x = 2;
    y = 3;
}
void MathTest::tearDown()
{
}
void MathTest::testAdd()
{
    int result = x + y;
    CPPUNIT_ASSERT( result == 5 );
}

void MathTest::testSub()
{
    int result = x + y;
    CPPUNIT_ASSERT( result == 1 );
}

/////////////////////////////////////////////////////////////////////
// Function: Main file for test
// DATE    : 2008-1-16
// Author  : xulinlin
// Vesion  : V1.0
// FileName: Main.cpp
// Compile : g++ -L /usr/local/lib/libcppunit.a Main.cpp MathTest.cpp -lcppunit -ldl -o MathTest
/////////////////////////////////////////////////////////////////////
#include <cppunit/extensions/TestFactoryRegistry.h>
//使用文本執(zhí)行器
#include <cppunit/ui/text/TestRunner.h>
// 如果不更改TestSuite, 本文件后期不需要更改.
int main()
{
        CppUnit::TextUi::TestRunner runner;
      
        // 從注冊(cè)的TestSuite中獲取特定的TestSuite, 沒有參數(shù)獲取未命名的TestSuite.
        CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
        // 添加這個(gè)TestSuite到TestRunner中
        runner.addTest( registry.makeTest() );
        // 運(yùn)行測(cè)試
        runner.run();
}

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