C++單元測試工具CppUnit使用簡介
準(zhǔn)備工作:
1. 到 http://sourceforge.net/projects/cppunit/下載CppUnit測試框架的源代碼
2. 解壓文件,進(jìn)入src文件夾,打開CppUnitLibraries.dsw.分別編譯這兩個(gè)project,輸出位置均為lib文件夾。
3.在VC的tools/options/directories/include files和library files中設(shè)置include路徑和lib路徑。
4.右鍵我的電腦設(shè)定->高級(jí)->環(huán)境變量->系統(tǒng)變量->Path中,將Cppuint的目錄的lib路徑設(shè)置為系統(tǒng)搜索路徑
創(chuàng)建測試工程:
1.打開VC新建基于MFC對(duì)話框的Project,在stdafx.h中添
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#ifdef _DEBUG
#pragma comment( lib, "testrunnerd.lib" )
#pragma comment( lib, "CppUnitd.lib" )
#else
#pragma comment( lib, "testrunner.lib" )
#pragma comment( lib, "CppUnit.lib" )
#endif
using namespace CppUnit;
2.修改對(duì)話框初始化函數(shù)
CUnitTestApp::InitInstance()
{
MfcUi::TestRunner runner;
runner.addTest( TestFactoryRegistry::getRegistry("SamepleUnitTest").makeTest() );
runner.run(); //show UI
/*
CUnitTestDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{......}
*/
}
3.將要測試的project加入到本project中,實(shí)現(xiàn)測試用例類
class CSamepleTestCase : public TestCase
{
public:
CSamepleTestCase(void);
virtual ~CSamepleTestCase(void);
CPPUNIT_TEST_SUITE(CSamepleTestCase);
CPPUNIT_TEST( TestAdd );
CPPUNIT_TEST_SUITE_END();
VOID TestAdd( );
};
在CPP文件中
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CSamepleTestCase,"SamepleUnitTest" );
#include <另一個(gè)工程中的頭文件>
void CSamepleTestCase::TestAdd( void )
{
//在這里寫測試用例
CPPUNIT_ASSERT_EQUAL( 1, 1);
}
來源:向明天進(jìn)軍0次