Preparatory work:
1. Download the source code of Cppunit test framework to http://sourceforge.net/projects/cppunit/
2. Extract files, enter the SRC folder, open cppunitlibraries.dsw. Compile the two project separately, the output location is the Lib folder.
3. Set include path and Lib path in VC tools/options/directories/include files and library files.
4. Right key My Computer settings-> advanced-> environment variable-> system variable->path, set the Cppuint directory's LIB path to the system search path
To create a test project:
1. Open VC new Project based on MFC dialog box, add in 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( );
};
In the CPP file
Cppunit_test_suite_named_registration (Csamepletestcase, "samepleunittest");
#include <另一个工程中的头文件>
void CSamepleTestCase::TestAdd( void )
{
//在这里写测试用例
CPPUNIT_ASSERT_EQUAL( 1, 1);
}