您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > cppUnit
CppUnit Cookbook中文版
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/1/2 10:04:17 ] 推薦標(biāo)簽:CppUnit 單元測(cè)試

TestFactoryRegistry

TestFactoryRegistry是用來(lái)解決以下兩個(gè)缺陷的:
*忘了把你的fixture suite加入test runner(因?yàn)樗诹硗庖粋(gè)文件中,很容易忘)
*因?yàn)榧尤胨袦y(cè)試用例頭文件造成的編譯瓶頸。

TestFactoryRegistry是在初始化的時(shí)候注冊(cè)suite的地方。

為了注冊(cè)ComplexNumber suite,在.cpp中加入:
#include <cppunit/extensions/HelperMacros.h>

CPPUNIT_TEST_SUITE_REGISTRATION( ComplexNumber );

事實(shí)上,桌面下的動(dòng)作是,一個(gè)靜態(tài)的AutoRegisterSuite類型變量被聲明。在構(gòu)建的時(shí)候,它會(huì)注冊(cè)一個(gè)TestSuiteFactory到TestFactoryRegistry。 TestSuiteFactory返回ComplexNumber::suite()返回的TestSuite。

為了運(yùn)行這些用例,使用文字的test runner,我們不必包含fixture了:
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main( int argc, char **argv)
{
CppUnit::TextUi::TestRunner runner;

首先我們得到TestFactoryRegistry的實(shí)例:
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
然后我們獲得并添加一個(gè)由TestFactoryRegistry產(chǎn)生的新的TestSuite,它包含了使用CPPUNIT_TEST_SUITE_REGISTRATION()注冊(cè)的所有的test suite.
runner.addTest( registry.makeTest() );
runner.run();
return 0;
}

Post-build check
好了,現(xiàn)在我們已經(jīng)可以使測(cè)試運(yùn)行了,那么把它集成到編譯過(guò)程中去怎么樣?
為了達(dá)到這個(gè)目的,應(yīng)用程序必須返回一個(gè)非0值表明出現(xiàn)了錯(cuò)誤。
TestRunner::run()返回一個(gè)布爾值來(lái)表明run()是否成功。
更新一下我們的main函數(shù),我們得到:
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main( int argc, char **argv)
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
bool wasSucessful = runner.run( "", false );
return wasSucessful;
}

現(xiàn)在,你需要編譯后運(yùn)行你的應(yīng)用程序。
使用 Visual C++的話,可以在Project Settings/Post-Build step中加入下面的命令。它被擴(kuò)展到應(yīng)用程序的執(zhí)行路徑。使用這個(gè)技術(shù)的時(shí)候看看project examples/cppunittest/CppUnitTestMain.dsp 中是如何設(shè)置的。

Original version by Michael Feathers. Doxygen conversion and update by Baptiste Lepilleur.

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