您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > cppUnit
CppUnit 快速使用指南
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/1/8 16:37:56 ] 推薦標(biāo)簽:開(kāi)發(fā) CppUnit 開(kāi)源

4. 常用使用方式

按照上面的方式,如果要添加新的測(cè)試用例,需要把每個(gè)測(cè)試用例添加到 TestSuite 中,而且添加新的 TestFixture 需要把所有頭文件添加到 main.cpp 中,比較麻煩。為此 CppUnit 提供了 CppUnit::TestSuiteBuilder,CppUnit::TestFactoryRegistry 和一堆宏,用來(lái)方便地把 TestFixture 和測(cè)試用例注冊(cè)到 TestSuite 中。下面是通常的使用方式:

/// MathTest.h
// A TestFixture subclass.
// Announce: use as your owner risk.
// Author  : liqun (liqun@nsfocus.com)
// Data    : 2003-7-5
#include "cppunit/extensions/HelperMacros.h"
class MathTest : public CppUnit::TestFixture {
 // 聲明一個(gè)TestSuite
 CPPUNIT_TEST_SUITE( MathTest );
 // 添加測(cè)試用例到TestSuite, 定義新的測(cè)試用例需要在這兒聲明一下
 CPPUNIT_TEST( testAdd );
 // TestSuite聲明完成
 CPPUNIT_TEST_SUITE_END();
 // 其余不變
protected:
 int m_value1, m_value2;
 
public:
 MathTest() {}
 
 // 初始化函數(shù)
 void setUp ();
 // 清理函數(shù)
 void tearDown();
 
 // 測(cè)試加法的測(cè)試函數(shù)
 void testAdd ();
 // 可以添加新的測(cè)試函數(shù)
};
/// MathTest.cpp
// A TestFixture subclass.
// Announce: use as your owner risk.
// Author  : liqun (liqun@nsfocus.com)
// Data    : 2003-7-5
#include "MathTest.h"
// 把這個(gè)TestSuite注冊(cè)到名字為"alltest"的TestSuite中, 如果沒(méi)有定義會(huì)自動(dòng)定義
// 也可以CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );注冊(cè)到全局的一個(gè)未命名的TestSuite中.
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MathTest, "alltest" );
// 下面不變
void MathTest::setUp()
{
     m_value1 = 2;
     m_value2 = 3;
}
void MathTest::tearDown()
{
}
void MathTest::testAdd()
{
     int result = m_value1 + m_value2;
     CPPUNIT_ASSERT( result == 5 );
}
/// main.cpp
// Main file for cppunit test.
// Announce: use as your owner risk.
// Compile : g++ -lcppunit MathTest.cpp main.cpp
// Run     : ./a.out
// Test    : RedHat 8.0 CppUnit1.8.0
// Author  : liqun ( a litthle modification.liqun@nsfocus.com)
// Data    : 2003-7-5
// 不用再包含所有TestFixture子類(lèi)的頭文件
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
// 如果不更改TestSuite, 本文件后期不需要更改.
int main()
{
 CppUnit::TextUi::TestRunner runner;
 
 // 從注冊(cè)的TestSuite中獲取特定的TestSuite, 沒(méi)有參數(shù)獲取未命名的TestSuite.
 CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
 // 添加這個(gè)TestSuite到TestRunner中
 runner.addTest( registry.makeTest() );
 // 運(yùn)行測(cè)試
 runner.run();
}

 
這樣添加新的測(cè)試用例只需要在類(lèi)定義的開(kāi)始聲明一下即可。

5. 其他實(shí)際問(wèn)題

通常包含測(cè)試用例代碼和被測(cè)試對(duì)象是在不同的項(xiàng)目中。應(yīng)該在另一個(gè)項(xiàng)目(好在不同的目錄)中編寫(xiě) TestFixture,然后把被測(cè)試的對(duì)象包含在測(cè)試項(xiàng)目中。

對(duì)某個(gè)類(lèi)或者某個(gè)函數(shù)進(jìn)行測(cè)試的時(shí)候,這個(gè) TestFixture 可能引用了別的類(lèi)或者別的函數(shù),為了隔離其他部分代碼的影響,應(yīng)該在源文件中臨時(shí)定義一些樁程序,模擬這些類(lèi)或者函數(shù)。這些代碼可以通過(guò)宏定義在測(cè)試項(xiàng)目中有效,而在被測(cè)試的項(xiàng)目中無(wú)效。

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