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

TestFactoryRegistry

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

TestFactoryRegistry是在初始化的時候注冊suite的地方。

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

CPPUNIT_TEST_SUITE_REGISTRATION( ComplexNumber );

事實(shí)上,桌面下的動作是,一個靜態(tài)的AutoRegisterSuite類型變量被聲明。在構(gòu)建的時候,它會注冊一個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();
然后我們獲得并添加一個由TestFactoryRegistry產(chǎn)生的新的TestSuite,它包含了使用CPPUNIT_TEST_SUITE_REGISTRATION()注冊的所有的test suite.
runner.addTest( registry.makeTest() );
runner.run();
return 0;
}

Post-build check
好了,現(xiàn)在我們已經(jīng)可以使測試運(yùn)行了,那么把它集成到編譯過程中去怎么樣?
為了達(dá)到這個目的,應(yīng)用程序必須返回一個非0值表明出現(xiàn)了錯誤。
TestRunner::run()返回一個布爾值來表明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í)行路徑。使用這個技術(shù)的時候看看project examples/cppunittest/CppUnitTestMain.dsp 中是如何設(shè)置的。

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

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