CPPUnit测试工程的建立

第一步:使用VC建立基于对话框的工程
工程名设为MyTest,存放在D:\MyTest。

工程设置尽可能简单:选择“在共享DLL中使用MFC”和“使用Unicode库”,其余一律不选。

第二步:加入CppUnit 库文件:

将CppUnit的库文件:cppunitd.lib,cppunitd_dll.lib,testrunnerd.lib加入到工程中。

第三步:设置CppUnit头文件和lib库文件路径、打开RTTI开关、给dLL库设置环境变量:
A)在VC中,点击ToolsOptionsDirectories中设置CppUnit 的include文件路径和lib文件路径:

B)点击菜单Project Project SettingsC++ Language:
打开RTTI开关。
C)拷贝TestRunnerd.dll
CppUnit提供一种基于GUI的测试环境,这个环境需要testrunnerd.dll文件,我们后面会使用CppUnit的这种GUI环境,所以,我们提前把这个库文件testrunnerd.dll拷贝到工程路径即D:\MyTest下。

第四步:屏蔽工程对话框
在工程MyTest.cpp文件中(即:工程名.cpp文件),
找到BOOL CMyTestApp::InitInstance()方法,屏蔽工程对话框。

BOOL CMyTestApp::InitInstance()
{
        AfxEnableControlContainer();

        // Standard initialization
        // If you are not using these features and wish to reduce the size
        //  of your final executable, you should remove from the following
        //  the specific initialization routines you do not need.

#ifdef _AFXDLL
        Enable3dControls();                        // Call this when using MFC in a shared DLL
#else
        Enable3dControlsStatic();        // Call this when linking to MFC statically
#endif

/*
        CMyTestDlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        if (nResponse == IDOK)
        {
                // TODO: Place code here to handle when the dialog is
                //  dismissed with OK
        }
        else if (nResponse == IDCANCEL)
        {
                // TODO: Place code here to handle when the dialog is
                //  dismissed with Cancel
        }
*/

        // Since the dialog has been closed, return FALSE so that we exit the
        //  application, rather than start the application's message pump.
        return FALSE;
}

第五步:实现CppUnit的GUI测试模式,并且创建CppUnit的执行器和测试工厂
A、在BOOL CMyTestApp::InitInstance()中,添加如下红色代码:

BOOL CMyTestApp::InitInstance()
{
        AfxEnableControlContainer();

        // Standard initialization
        // If you are not using these features and wish to reduce the size
        //  of your final executable, you should remove from the following
        //  the specific initialization routines you do not need.

#ifdef _AFXDLL
        Enable3dControls();                        // Call this when using MFC in a shared DLL
#else
        Enable3dControlsStatic();        // Call this when linking to MFC statically
#endif

        //定义CppUnit的测试执行器
        CppUnit::MfcUi::TestRunner runner; 

        //CppUnit为被测对象定义一个测试工厂(这里取名叫FactoryTest):
        CppUnit::TestFactoryRegistry &registry
= CppUnit::TestFactoryRegistry::getRegistry("FactoryTest");

        //并将工厂添加到测试执行器中
    runner.addTest( registry.makeTest() );

        //运行执行器,显示执行器GUI界面
        runner.run(); 

        /*
        CMyTestDlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        if (nResponse == IDOK)
        {
                // TODO: Place code here to handle when the dialog is
                //  dismissed with OK
        }
        else if (nResponse == IDCANCEL)
        {
                // TODO: Place code here to handle when the dialog is
                //  dismissed with Cancel
        }
        */

        // Since the dialog has been closed, return FALSE so that we exit the
        //  application, rather than start the application's message pump.
        return FALSE;
}

继续执行红色感叹号!会发现程序报很多的错,为什么?
因为我们刚才加入的代码使用了CppUnit类,但这个类在哪里?系统并不知道,所以还需在这个文件头加入该类的头文件(不用怕,这些头文件不用我们自己去找,CppUnit工具手册中已有说明)
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/mfc/TestRunner.h>

第六步:定义测试套文件,向测试套中添加测试用例
做完上面的操作后,执行红色感叹号!你会看到什么?
如果你前面操作无误的话,你会看到如下界面:
注意:我们需要的GUI界面出来了,点击Browse,可以看到测试工厂,但是没有测试套,
自然也无法向测试套添加测试用例,也就是说,现在还不能通过用例测试IsCodeLine函数。
那好,让我们先来添加测试套:
按照下面示图加入测试类的测试套头文件和测试套源文件,文件名可以随便,我们这里使用TestSuite.h和TestSuite.cpp:

TestSuite.h中的代码如下:

#include "cppunit/extensions/HelperMacros.h"

class myTestSuite : public CppUnit::TestFixture {

        CPPUNIT_TEST_SUITE(myTestSuite);   // 声明一个TestSuite,名称myTestSuite
        CPPUNIT_TEST( testcase_001);        //这里向测试套里面添加测试用例
        CPPUNIT_TEST_SUITE_END();         // TestSuite声明完成

public:
        /*
        这里应该定义你的测试用例
        */
                // 定义测试用例
        void testcase_001();

};

TestSuite.cpp中的代码如下(注意头文件要做相应的修改):

#include "stdafx.h"

#include "TestSuite.h"

// 把TestSuite"测试套添加到测试工厂中,测试工厂"FactoryTest"

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(myTestSuite,"FactoryTest" );

#define RET_OK 0
#define RET_FAIL 1
void myTestSuite::testcase_001()
{
//这是一个测试用例,但是这个用例里面现在是空的
}

提供的断言:

CPPUNIT_ASSERT(condition) // 确信condition为真
CPPUNIT_ASSERT_MESSAGE(message, condition) // 当condition为假时失败, 并打印message
CPPUNIT_FAIL(message) // 当前测试失败, 并打印message
CPPUNIT_ASSERT_EQUAL(expected, actual) // 确信两者相等
CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual) // 失败的同时打印message
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta) // 当expected和actual之间差大于delta时失败 
原文地址:https://www.cnblogs.com/lilun/p/1770433.html