准备工作:
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);
}