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

       我們首先新建一個頭文件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 );//針對構造函數(shù)Money( double amount, std::string currency )
  CPPUNIT_TEST( testEqual );//針對bool operator ==( const Money &other ) const
  CPPUNIT_TEST( testAdd );//針對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ù)進行編寫case,每個case盡量獨立,這樣便于維護,每個函數(shù)盡量自己初始化類然后釋放資源不應對其他測試用例造成影響,是要減小依賴性,在測試過程中我們常常碰到依賴性的問題,比如原來過的你修改了一個變量對后來的問題造成不過了,這是你要檢查的,比如你是否每個對象之間是否是維護同一個內(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()//我們在這里可以建立統(tǒng)一的測試環(huán)境,比如初始化一些變量,全局量,構造一些測試對象
{
}


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

{
}

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