C + + Unit Test Tool Cppunit use Introduction

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.