准備工作:
1. 到 http://sourceforge.net/projects/cppunit/下載CppUnit測試框架的源代碼
2. 解壓文件,進入src文件夾,打開CppUnitLibraries.dsw.分別編譯這兩個project,輸出位置均為lib文件夾。
3.在VC的tools/options/directories/include files和library files中設置include路徑和lib路徑。
4.右鍵我的電腦設定->高級->環境變量->系統變量->Path中,將Cppuint的目錄的lib路徑設置為系統搜索路徑
創建測試工程:
1.打開VC新建基於MFC對話框的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.修改對話框初始化函數
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中,實現測試用例類
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 <另一個工程中的頭文件>
void CSamepleTestCase::TestAdd( void )
{
//在這裡寫測試用例
CPPUNIT_ASSERT_EQUAL( 1, 1);
}