Configure and use CPPUnit notes

xiaoxiao2021-03-05  25

First, the method of configuring CPPUnit

Compile

1. Compile the CPPUnit_dll project in $ base / src / cppunit / cppunitlibraries.dsw, the Release version generates cppUnit_dll.lib and cppUnit_dll.dll, the Debug version generates cppunitd_dll.lib and cppunitd_dll.dll. This is the CPPUnit basic class library.

2. Compile the Testrunner project in $ BASE / SRC / CPPUnit / CPPUnitlibraries.dsw. Release version generates Testrunner.lib and Testrunner.dll, the Debug version generates Testrunnerd.lib and Testrunnerd.dll. This is the class library using the MFC graphical interface.

3. Place all LIB files below the CommonFiles / Lib directory.

4. Place the DLL file in the corresponding debug and release executable directory.

Set VC

1. Set the header file path of CPPUnit in the Option of VC. It is a $ base / include directory.

2. Add in stdafx.h

#ifndef _debug

#pragma comment (Lib, "..///cppunit_dll.lib")

#pragma comment (lib, "..////commonfiles/lib//testrunner.lib")

#ELSE

#pragma comment (LIB, "..///cppunfiles/lib//cppunitd_dll.lib")

#pragma comment (lib, "..////commonfiles/lib/testrunnerd.lib")

#ENDIF

Because Debug and Release version are different from the library

Second, the use of cppunit without using the auxiliary macro

Newly built a dialog-based EXE program.

Test class

Create a test class CPLUSTEST:

#pragma Warning (Push, 3)

#include "../../cppunit/include/cppunit/testfixture.h"

#include "../../cppunit/include/cppunit/teststert.h"

#pragma Warning (POP)

Class CPlustest: Public CPPUnit :: TestFixTure

{

PUBLIC:

CPlustest ();

Virtual ~ cplustest ();

Void setup ()

{

X = 1;

y = 2;

}

Void Teardown ()

{

X = 0;

y = 0;

}

Void Testequal1 ()

{

CPPUNIT_ASSERT (x y == 4);

}

Void TESTEQUAL2 ()

{

CPPUNIT_ASSERT (x y == 3);

}

Private:

INT X;

Int Y;

}

Description:

The test class typically inherits from TestFixTure, which is used to represent a test object to organize test cases.

Before each test case is executed, the setup of the class is executed; after it is the TEARDOWN it belongs to. For this example, SETUP will be executed before running Testequal1, and Teardown will be executed before running TESTEQUAL2, and Teardown will be executed later. CPPUNIT_ASSERT is used to determine the value in parentheses is TRUE or FALSE. If TRUE, the test is successful; otherwise the test failed. There are also some other macros to use.

Main function

Add the following containing statements in the main CPP file

#pragma Warning (Push, 3)

#include "../../cppunit/include/cppunit/ui/mfc/testrunner.h"

#include "../../cppunit/include/cppunit/testcaller.h"

#include "../../cppunit/include/cppunit/testresult.h"

#include "../../cppunit/include/cppunit/testsuite.h"

#pragma Warning (POP)

Add the following code to InitInstance and remove the code of the dialog box before display

CPPUnit :: MFCUI :: Testrunner Runner;

CPPUnit :: TestSuite Suite ("Test");

CPPUnit :: TestCaller Test1 ("Testequal1",

& CPlustest :: Testequal1);

CPPUnit :: TestCaller Test2 ("Testequal2",

& Cplustest :: Testequal2);

Suite.addtest (& Test1);

Suite.addtest (& Test2);

Runner.addtest (& suite);

Runner.run ();

Description: Create a Test Suite called TEST. Then establish two Test Cases, named TESTEQUAL1 and TESTEQUAL2, respectively, corresponding test functions, respectively, CPlustest :: Testequal1 and CPlustest :: Testequal2.

Then add two test examples to the test Suite.

Finally, add the test Suite to Runner. The Runner here is an MFC graphical interface, or it can be text.

With graphical interfaces, Test Suite and Test Case correspond to branches and leaves, and Runner corresponds to the root. Test Suite can include other Test Suite, runners can also contain Test Case.

Third, using auxiliary macro CPPUNIT usage method (recommended)

The above method must do a lot of Test Suite and Test Case operations in the main function. For convenience, CPPUnit provides some macros.

Improvement of test class

In the declaration of the test class, add the following four sentences.

CPPUNIT_TEST_SUITE (CPLUSTEST);

CPPUNIT_TEST (TESTEQUAL1);

CPPUNIT_TEST (TESTEQUAL2);

CPPUNIT_TEST_SUITE_END ();

Explanation: To the top of the class, you will add these statements in front of public. The TESTEQUAL1 and TESTEQUAL2 of this class are placed in a Test Suite as a Test Case, respectively. The name of this test suite is the name of the class, that is, "CPLUSTEST"

In the beginning of the corresponding CPP file, add the following statement

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION (CPlustest, "Plustest");

Note: Expressing Suite ("CPLUSHTEST") to add this class to a name of Plustest's Test Suite. At the same time, Plustest Suite will be registered. Alternatively, cppUnit_test_suite_registration (CPLUSTEST) can also be used; the corresponding Suite to the default Test Suite ("All Tests").

Only need to include files

#include "../../cppunit/include/cppunit/extensions/helpermacros.h"

Main function improvement

I only need Testrunner.h in the main function.

Only the following statement is required in InitInstance

CPPUnit :: MFCUI :: Testrunner Runner;

CPPUnit :: Test * Psuite =

CPPUnit :: TestFactoryRegistry :: getRegistry ("plustest"). Maketest ();

Runner.addtest (PSuite);

Runner.run ();

Description:

The three-layer tree structure is now included in the graphical interface. The first layer is PLUSTEST, the second layer is CPlustest, and the third layer is TestPlus1 and TestPlus2.

You can give GetRegistry not specifying parameters using the default registration Test Case, "All Tests".

转载请注明原文地址:https://www.9cbs.com/read-36043.html

New Post(0)