您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > cppUnit
CPPUnit實(shí)例Simple class
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/15 14:10:45 ] 推薦標(biāo)簽:

       我們首先新建一個(gè)頭文件MoneyTest.h,其內(nèi)容如下:
// MoneyTest.h
#ifndef MONEYTEST_H
#define MONEYTEST_H

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CPPUNIT_NS::TestFixture
{
  CPPUNIT_TEST_SUITE( MoneyTest );
  CPPUNIT_TEST( testConstructor );//針對(duì)構(gòu)造函數(shù)Money( double amount, std::string currency )
  CPPUNIT_TEST( testEqual );//針對(duì)bool operator ==( const Money &other ) const
  CPPUNIT_TEST( testAdd );//針對(duì)Money &operator +=( const Money &other )
 
  CPPUNIT_TEST_SUITE_END();

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

  void testConstructor();
  void testEqual();
  void testAdd();

};

#endif  // MONEYTEST_H

        5. 編寫case的時(shí)間到了,我們針對(duì)每個(gè)函數(shù)進(jìn)行編寫case,每個(gè)case盡量獨(dú)立,這樣便于維護(hù),每個(gè)函數(shù)盡量自己初始化類然后釋放資源不應(yīng)對(duì)其他測(cè)試用例造成影響,是要減小依賴性,在測(cè)試過程中我們常常碰到依賴性的問題,比如原來(lái)過的你修改了一個(gè)變量對(duì)后來(lái)的問題造成不過了,這是你要檢查的,比如你是否每個(gè)對(duì)象之間是否是維護(hù)同一個(gè)內(nèi)存(變量)了。


// MoneyTest.cpp

#include "StdAfx.h"
#include <cppunit/config/SourcePrefix.h>
#include "Money.h"
#include "MoneyTest.h"

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );


void
MoneyTest::setUp()//我們?cè)谶@里可以建立統(tǒng)一的測(cè)試環(huán)境,比如初始化一些變量,全局量,構(gòu)造一些測(cè)試對(duì)象
{
}


void
MoneyTest::tearDown()//清除環(huán)境,這在每個(gè)測(cè)試用例執(zhí)行完之后都會(huì)進(jìn)行的動(dòng)作,這個(gè)非常關(guān)鍵,不然可能會(huì)造成意想不到的結(jié)果

{
}

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