Use cppunit to test the MFC Program

Source: Internet
Author: User

 

1. Download Resources

 

The use of cppunit-1.12.1, download online after you need to compile your own, the compilation process may have a variety of problems

Here, you can directly use the compiled software in the resource:

Http://download.csdn.net/detail/shuilan0066/4395617, skip the recompilation process.

 

Resources also contain cppunitprojectwizard

 

2. Environment Configuration

Cppunit cannot be used directly. You must configure the runtime environment first.

 

Cppunit environment Configuration:

For example, cppunit is installed on the E: \ cppunit-1.12.1

 

1) define the environment variable cppunitdir

Variable value: e: \ cppunit-1.12.1

2) modify the environment variable path. Add % cppunitdir %/lib at the end of the original path so that cppunit_dll.dll can be found during program loading.

 

3) set the include/lib path in the development environment.

 

Add include: $ (cppunitdir) \ include

 

Add Lib: $ (cppunitdir) \ Lib to the additional library directory under project --> Property --> Connector

Cppunitprojectwizard environment Configuration:

1) after decompression, copy the following two files to the vcprojects folder under the Visual Studio 8 installation directory.
... Under/VC/vcprojects

Cppunitprojectwizard. vsdir-
Name the wizard

Cppunitprojectwizard. vsz-
Let vs know where to find the wizard

 

2) copy the entire cppunitprojectwizard solution folder to the vcwizards folder under your Visual Studio 8 installation directory. For example, place it in... Under/VC/vcwizards

Then, use NotePad to edit cppunitprojectwizard. vsz and define the parameter absolute_path.

Param = "absolute_path =... VC/vcwizards/cppunitprojectwizard"

3 cppunit macro description

Cppunit uses the following macro to determine whether the program is correct.

Cppunit_assert is a macro that determines whether the following parameters are correct. cppunit has many macros, such as cppunit_assert (condition) // make sure that condition is true cppunit_assert_message (message, condition) // failed when condition is false, Print message cppunit_fail (Message) // current test failed, and print message cppunit_assert_equal (expected, actual) // make sure the two are equal cppunit_assert_receiv_message (message, expected, actual) // Print message cppunit_assert_doubles_equal (expected, actual, Delta) when the difference between expected and actual is greater than Delta

 

4. Simple Example

 

1) Open vs2005 and create an MFC dialog box named
Cppunit

2) Set Project Properties

 

Add include: $ (cppunitdir) \ include

 

Add Lib: $ (cppunitdir) \ Lib to the additional library directory under project --> Property --> Connector

 

 3) Open the cppunit. cpp file and add the library code that contains files and declarations.

 

#include <cppunit/ui/mfc/TestRunner.h>#include <cppunit/extensions/TestFactoryRegistry.h>#pragma comment(lib, "cppunitd.lib")#pragma comment(lib, "testrunnerud.lib")

 

NOTE: If it is a release version declaration, the library file is

 

#pragma comment(lib, "cppunit.lib")#pragma comment(lib, "testrunner.lib")

4) modify the initinstance () function

Commented out the dialog box

Cppunit_ns test added

Bool ccppunitapp: initinstance () {// If an Application List running on Windows XP specifies that you want to use comctl32.dll version 6 or later to enable visualization, // you need initcommoncontrolsex (). Otherwise, a window cannot be created. Initcommoncontrolsex initctrls; initctrls. dwsize = sizeof (initctrls); // set it to include all the // public control classes to be used in the application. Initctrls. dwicc = icc_win95_classes; initcommoncontrolsex (& initctrls); cwinapp: initinstance (); afxenablecontrolcontainer (); // standard initialization // if these functions are not used and you want to reduce the size of the final executable file, remove the following // unnecessary initialization routine // modify the registry key used to store the settings // todo: Modify the string as appropriate, // For example, modify it to the company or organization name setregistrykey (_ T ("Local Application generated by the Application Wizard"); // comment out this section // ccppunitdlg DLG; // m_pmainwnd = & DLG; // int_ptr nresponse = DLG. domodal (); // If (nresponse = idok) // {// todo: Place When to use "OK" to close the // dialog box code //} // else if (nresponse = idcancel) // {// todo: when to use "cancel" to close the // code of the dialog box /// as the dialog box is closed, false is returned to exit the application, // instead of the message pump that starts the application. // Add the following code cppunit_ns: mfcui: testrunner runner; runner. addtest (cppunit_ns: testfactoryregistry: getregistry (). maketest (); runner. run (); Return false ;}

Compile and run the unit test example interface shown in the following figure:

 

 

 

5) add test code

A) add a C ++ class example whose base class is public.
Cppunit_ns: testfixture

B)
Modify example. h

 

#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);};

 

C) Modify example. C.

 

#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}

 

D) Compile at this time and you will find a test package:

 

E) add the test package initialization, end function setup, and teardown (case sensitive ). And add test cases testcase1 and testcase2.

Example. h

#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST(testcase1);CPPUNIT_TEST(testcase2);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);public:void setUp();void tearDown();void testcase1();void testcase2();};

Example. cpp

#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}void example::testcase1(){AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);}void example::testcase2(){AfxMessageBox((LPCTSTR)_T("I am testcase2"),MB_OK,0);}void example::setUp(){AfxMessageBox((LPCTSTR)_T("I am setup"),MB_OK,0);}void example::tearDown(){AfxMessageBox((LPCTSTR)_T("I am teardown"),MB_OK,0);}

Every use case calls setup and teardown. This is the default internal cppunit.

 

F) add other Use Cases

#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST(testcase1);CPPUNIT_TEST(testcase2);CPPUNIT_TEST(testcase3);CPPUNIT_TEST(testcase4);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);public:void setUp();void tearDown();void testcase1();void testcase2();void testcase3();void testcase4();};

 

#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}void example::testcase1(){AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);}void example::testcase2(){AfxMessageBox((LPCTSTR)_T("I am testcase2"),MB_OK,0);}void example::testcase3(){CPPUNIT_ASSERT(1==1);}void example::testcase4(){CPPUNIT_ASSERT(1==2);}void example::setUp(){AfxMessageBox((LPCTSTR)_T("I am setup"),MB_OK,0);}void example::tearDown(){AfxMessageBox((LPCTSTR)_T("I am teardown"),MB_OK,0);}

 

G) unit test results

If all assertions in the test are passed, the result is green.

If an asserted failure occurs, the error location is displayed in red, and the error location is displayed below. You can double-click the source code location.

 

 

5. Test the code in another project.

 

Create a project dlgtest in the dialog box, and check the function cdlgtestdlg: add in the project.

1) Put the cppunit test project and the tested project in the same directory and open it in the same solution

2) set the test environment and add the path of the tested project to the additional include path.

For example, the path of the tested project dlgtest is: e: \ test \ dlgtest

Add E: \ test \ dlgtest

Because the include path of cppunit was added before

Therefore, the value after adding the tested path is: "$ (cppunitdir) \ include"; E: \ test \ dlgtest

3) use the add-> function to add the tested dlgtestdlg. h and dlgtestdlg. cpp to cppunit.

 

4) modify the test unit example in the cppunit unit test project.

 

Modify example. h

Add the header file of the function to be tested

#include "DlgTestDlg.h"

Modify example. cpp

Here, test the Add function in the cdlgtestdlg dialog box.

void example::testcase1(){//AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);CDlgTestDlg dlg;int i=dlg.Add(3,5);    CPPUNIT_ASSERT(i==9);}

5) Compile and run the command to view the test results.

 

Cppunit test project template is saved in the Resource
Http://download.csdn.net/detail/shuilan0066/4396670, which can be downloaded, placed under the equivalent directory of the project under test, modify the parameters for testing

 

References:

Http://blog.csdn.net/ainiyidiandian/article/details/5788240

Http://morningspace.51.net/resource/cppunit/cppunit_anno.html

Http://www.cppblog.com/ace/archive/2006/08/04/10842.html

Http://blog.csdn.net/wincol/article/details/5789644

Http://blog.csdn.net/alicehyxx/article/details/4483681

Http://blog.csdn.net/jinjunmax_78/article/details/2033755

 

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.