您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 >
用cpp做C單元測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/2/19 16:17:19 ] 推薦標(biāo)簽:

編寫(xiě)測(cè)試用例

  一旦我們知道我們要測(cè)什么之后,我們可以寫(xiě)測(cè)試用例了。我們能夠執(zhí)行所有的我們需要的操作:使用普通庫(kù)函數(shù),第三方庫(kù),win32api庫(kù)函數(shù),或簡(jiǎn)單使用c++內(nèi)部操作

  有時(shí)候,我們需要調(diào)用外部輔助文件或者數(shù)據(jù)庫(kù),比較外部文件和內(nèi)部數(shù)據(jù)是否一致。

  每發(fā)現(xiàn)一個(gè)錯(cuò)誤時(shí)9比如發(fā)現(xiàn)內(nèi)部數(shù)據(jù)和外部數(shù)據(jù)不同我們創(chuàng)建一個(gè)異常,使用 CPPUNIT_FAIL(message) 來(lái)顯示異常信息。

  檢測(cè)一個(gè)條件使用
CPPUNIT_ASSERT(condition):如果為false拋出異常
CPPUNIT_ASSERT_MESSAGE(message, condition): 如果為false拋出制定的信息。
CPPUNIT_ASSERT_EQUAL(expected,current): 檢測(cè)期望值
CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,current): 當(dāng)比較值不相等時(shí)候拋出的制定的信息。
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,current,delta): 帶精度的比較
  下面是測(cè)試loadTest的例子,
//
// These are correct values stored in auxiliar file
//
#define AUX_FILENAME "ok_data.dat"
#define FILE_NUMBER 19
#define FILE_STRING "this is correct text stored in auxiliar file"

void DiskDataTestCase::loadTest()
{
// convert from relative to absolute path
TCHAR absoluteFilename[MAX_PATH];
DWORD size = MAX_PATH;

strcpy(absoluteFilename, AUX_FILENAME);
CPPUNIT_ASSERT( RelativeToAbsolutePath(absoluteFilename, &size) );

// executes action
CPPUNIT_ASSERT( fixture->load(absoluteFilename) );

// ...and check results with assertions
LPDATA loadedData = fixture->getData();

CPPUNIT_ASSERT(loadedData != NULL);
CPPUNIT_ASSERT_EQUAL(FILE_NUMBER, loadedData->number);
CPPUNIT_ASSERT( 0 == strcmp(FILE_STRING,
fixture->getData()->string) );
}

  在這個(gè)case我們得到四個(gè)可能的錯(cuò)誤:

load method's return value
getData method's return value
number structure member's value
string structure member's value

  第二個(gè)用例也是相似的。但是困難點(diǎn),我們需要使用已知的數(shù)據(jù)來(lái)填充fixture,把它存在磁盤(pán)臨時(shí)文件里,然后打開(kāi)兩個(gè)文件(新的和輔助文件),讀并比較內(nèi)容,兩者如一致正確

void DiskDataTestCase::storeTest()
{
DATA d;
DWORD tmpSize, auxSize;
BYTE *tmpBuff, *auxBuff;
TCHAR absoluteFilename[MAX_PATH];
DWORD size = MAX_PATH;

// configures structure with known data
d.number = FILE_NUMBER;
strcpy(d.string, FILE_STRING);

// convert from relative to absolute path

strcpy(absoluteFilename, AUX_FILENAME);
CPPUNIT_ASSERT( RelativeToAbsolutePath(absoluteFilename, &size) );

// executes action
fixture->setData(&d);
CPPUNIT_ASSERT( fixture->store("data.tmp") );

// Read both files contents and check results
// ReadAllFileInMemory is an auxiliar function which allocates a buffer
// and save all file content inside it. Caller should release the buffer.
tmpSize = ReadAllFileInMemory("data.tmp", tmpBuff);
auxSize = ReadAllFileInMemory(absoluteFilename, auxBuff);

// files must exist
CPPUNIT_ASSERT_MESSAGE("New file doesn't exists?", tmpSize > 0);
CPPUNIT_ASSERT_MESSAGE("Aux file doesn't exists?", auxSize > 0);

// sizes must be valid
CPPUNIT_ASSERT(tmpSize != 0xFFFFFFFF);
CPPUNIT_ASSERT(auxSize != 0xFFFFFFFF);

// buffers must be valid
CPPUNIT_ASSERT(tmpBuff != NULL);
CPPUNIT_ASSERT(auxBuff != NULL);

// both file's sizes must be the same as DATA's size
CPPUNIT_ASSERT_EQUAL((DWORD) sizeof(DATA), tmpSize);
CPPUNIT_ASSERT_EQUAL(auxSize, tmpSize);

// both files content must be the same
CPPUNIT_ASSERT( 0 == memcmp(tmpBuff, auxBuff, sizeof(DATA)) );

delete [] tmpBuff;
delete [] auxBuff;

::DeleteFile("data.tmp");
}


調(diào)用用戶接口

  后,我們看看用一個(gè)mfc 對(duì)話框(TestRunner.dll)用來(lái)說(shuō)明。

  我們需要在我們的初始化函數(shù)中做如下初始化

#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

BOOL CMy_TestsApp::InitInstance()
{
....

// declare a test runner, fill it with our registered tests and run them
CppUnit::MfcUi::TestRunner runner;

runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );

runner.run();

return TRUE;
}

  只要定義一個(gè)test的實(shí)例,然后注冊(cè)所有用例,在跑case。

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