9CBS CC ++ Electronic Magazine First CPPUnit Test Framework Getting Started

xiaoxiao2021-03-06  34

□ Treatment

Text / CPLUSER (Clean Code That Works.)

Demo code download

Test Drive Development (TDD, Test-Driven Development) is a center of test as a development process, which insists that before writing actual code, write the test code based on product code. The goal of the development process is to first pass the test, and then optimize the design structure. Test drive development is an important part of extreme programming. XUnit, a test framework based on test-driven development, which provides a convenient tool for us to use test driver development during the development process, so that we can quickly perform unit testing. Members of xUnit have a lot, such as JUnit, PythonUnit, etc. CPPUnit, which is introduced to you today is a member of the XUnit family, which is a test frame for C .

This article does not introduce a detailed introduction to the CPPUnit source code, but only for CPPUnit's application. In this article, you will see:

1. Each component of the cppUnit source code.

2. How to set your development environment to use CPPUnit.

3, how to add test code for your product code (actually in turn, add product code for test code. In TDD, there is a product code after test code) and test it through CPPUnit.

The background of this document is: CPPUNIT1.9.0, Visual C 6.0, Windows2000. The statement is incorrect in the article, please criticize and correct.

First, CPPUnit source code composition

The source code for the CPPUnit test frame can be downloaded to http://sourceforge.net/projects/cppunit/. After downloading the decompression, you will see the following folder:

figure 1

The main folders are:

DOC: CPPUnit instructions documentation. In addition, the root directory of the code, there are three documentation, named INSTALL, INSTALL-UNIX, INSTALL-WIN32.TXT.

Examples: CPPPUnit provides an example of CPPUnit itself, which can learn how to develop using the CPPUnit test framework.

Include: CPPUnit header file.

SRC: CPPUnit source code directory.

Second, the first CPPUnit test environment

After decompressing the source code package, you must have anxious to see what CPPUnit is? OK, let's unveil the mystery of CPPUnit.

1. Enter the Example folder and open Examples.dsw with VC. Let's first take a look at the test example of the CPPUnit. These examples are for CPPUNIT itself unit test set, on the one hand, the CPPUnit author develops test cases written during the CPPUnit framework process, on the other hand, we can learn how to add test cases in our own project.

2. Set the CPPUNITTESTAPP project to Active Project (Win32 Debug), run after compiling, you can see the interface of the CPPUnit-based GUI mode for unit test Testrunner. Click "Run" and will see the interface as shown in Figure 2:

figure 2

image 3

This is a unit test result for CPPUnit, which shows that we have made 11 tests, all through.

Click "Browse", we can also choose the unit test you want to do, as shown in Figure 3:

CPPUnit Tests all units in accordance with the structure of the tree. In CPPUnit, the minimum test unit is called a TestMethod test method, while multiple related test methods can form a TestCase test case. Multiple test cases also form a TestSuite test package. The test package is nesting together, forming the tree structure we have seen above. We can choose any of the tree nodes to perform unit testing. 3. Set the CPPUNITTESTMAIN project to Active Project (Win32 Debug), compile and run, let's take a look at another environment of another unit test, as shown in Figure 4:

Figure 4

This is a text-based unit test environment. CPPUnit provides several test environments, one based on text, one based on GUI, that is, Figure 3.

4. Set the HostApp project to Active Project (Win32 Debug), compile operation. Figure 5:

Figure 5

This is also a test conducted by CPPUnit itself, but it demonstrates it to we demonstrate a variety of failed tests. In the GUI-based test environment, if the test is unsuccessful, the progress bar shows red, and it is green. From test results We can see the failure unit test name, causing the reason why the test cannot pass, as well as the files and the number of files where the test failed statement is located.

Third, CPPUNIT development environment settings

Know the CPPUnit's test environment, you must have a feeling of tested drive development during your development process. However, before using CPPUnit, you need to set your development environment.

1, CPPUnit's LIB and DLL

CPPUnit offers we with two sets of frameworks, a static lib, a dynamic DLL.

CPPUnit Project: Static LIB

CPPUNIT_DLL Project: Dynamic DLL and LIB

In the development we can make a choice according to the actual situation. Enter the SRC folder to open CPPUnitLibraries.dsw. Compile these two Project, the output position is a lib folder.

Another Project that needs attention is Testrunner, which outputs a DLL that provides a GUI-based test environment, which is one of the two test environments we mentioned earlier. We also need to compile this Project, and the output location is also a lib folder.

To facilitate development, we use these compiled libs and dlls (including Debug and Release Edition) Copy to our own folder (of course, you can don't do this), such as f: /cppUnit1.9.0/lib / And we also also put the CPPUnit source code COPY to our own include folder. Then set the include path and lib path in the Tools / Options / Directories / Include Files / Directories / Include Files and Library Files. Don't forget the correct LIB in your Project.

2, open the RTTI switch in your VC Project.

Specific location Project settings / C / C language.

3, set environment variables for Testrunner.dll

Testrunner.dll provides us with a GUI-based test environment. In order to make our test program call it correctly, Testrunner.dll must be in the path of your test program. But the easiest way is to add Testrunner.dll's path to the path to Testrunner.dll in the environment variable path of the operating system.

Four, your first TDD EXAMPLE

Everything is ready, now we can take a look at how to add a test code. As we mentioned, the minimum test unit of CPPUnit is Testcase, and multiple related TestCase form a testsuite. The simplest way to add test code is to use CPPUnit to make several macro we provide (of course, there are other manual joining methods, but all the same way, everyone can check the demo code in the CPPUnit header file). These macros are: cppUnit_test_suite () Started to create a TestSuite

CPPUNIT_TEST () Add Testcase

CPPUNIT_TEST_SUITE_END () End Create TestSuite

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION () Add a TestSuite to a specified TestFactoryRegistry plant.

Interested friends can take a look at these macro in Helpermacros.h, which is not described herein.

1, a class that implements two integers

Assuming that we have to implement a class, class name is temporarily taking CPLUS, it is mainly to achieve two numbers (more simple classes, do you test? Don't tighten, we just know how to join the test code to test It's okay, the more simple and better. Assume that the additional method to be implemented is:

Int Add (int NNum1, int NNUM2);

OK, let's write the code to test this method. TDD is written first, after writing product code (CPLUS)! The test code written first is often unable to run or compile. Our goal is to write product code after writing test code, making it compiled, and then reconstructing. This is the "Red / Green / Refactor" that Kent Beck (Remember the Status section of the GUI-based test environment?). Therefore, the above class name and method should be just in your heart, but it is just your IDEA.

2, create a Project for test code in VC

Typically, the test code and the test object are in different Project. This will not let your product code "pollution" "pollution" is tested.

In this example, we will establish a GUI-based test environment. In VC, we build a dialog-based Project. Don't forget the correct LIB of LINK. In this case, we use static cppunit lib. Since we want this Project running to display the interface of Figure 2, we need to block the original dialog box in the app's initInstance (), in which CPPUNIT's GUI.

CPPUnit :: MFCUI :: Testrunner Runner;

Runner.addtest (Plustest :: Suite ()); // Add test

Runner.run (); // show ui

/ *

CCPLUSTESTDLG 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

}

* /

As we mentioned, Testrunner outputs the dialog box in Figure 2, which is why we want to set the environment variable for the path to Testrunner.dll.

Note: Plustest :: Suite () Returns a pointer to CPPUnit :: test. This pointer is the starting point of the entire test. CPPUnit :: TestFactoryRegistry :: getRegistry () Returns the TestFactoryRegistry factory according to the name of TestSuite, and then calls MakeTest () in the factory to assemble the TestSuite. This is a recursive call, which will establish a tree test structure. Namespace Plustest

{

CPPUnit :: test * suite ()

{

CPPUnit :: TestFactoryRegistry & registry =

CPPUnit :: TestFactoryRegistry :: getRegistry (Plussuitename ());

Return regiSTRY.MAKETEST ();

}

}

Don't forget to add a header:

#include "cplustestsuite.h" #include #include

3, add a class in Project, name CPlustestcase

CPlustestcase inherits from cppunit :: testcase, the code is as follows:

Class CPlustestcase: PUBLIC CPPUnit :: Testcase

{

CPPUNIT_TEST_SUITE (CPLUSTESTSE);

CPPUNIT_TEST (TESTADD);

CPPUNIT_TEST_SUITE_END ();

PUBLIC:

CPlustestCase ();

Virtual ~ cplustestcase ();

Void testAdd (); // Test method

}

Have you seen these many macro? They have a big hand.

CPPUNIT_TEST_SUITE (CPLUSTESTSE);

CPPUNIT_TEST (TESTADD);

CPPUNIT_TEST_SUITE_END ();

With these macros, we registered CPlustestCase and TestAdd to the test list.

In addition, we need to add another macro in the CPP file:

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION (CPlustestcase, Plustest :: plussUitename ());

It registers CPlustestCase TestSuite into a specified TestFactory factory, which is identified by the name returned by the Plustest :: plusuitename () function (the suite () function introduced earlier is through this name). Plussuitename () is a function under this Plustest's nameSpace, which returns to our name created for this TestSuite (this example we are named "Plus"). In fact, we can also do this, you can write "Plus" directly in Hongli. But this can prevent the trouble of hard coding.

In the test class, we added a test method:

Void TestAdd ();

The object it tested is a method for the CPLUS class mentioned earlier:

Int Add (int NNum1, int NNUM2);

Let's take a look at its implementation:

Void CPlustestcase :: TestAdd ()

{

CPlus Plus;

INT NRESULT = Plus.Add (10, 20); // Execute Add operation

CPPUNIT_ASSERT_EQUAL (30, NRESULT); // The check result is equal to 30}

CPPUNIT_ASSERT_EQUAL is a macro of a judgment result. Similar other macros in CPPUnit See Testassert.h, this article does not detail here.

In addition, we can override the setup (), Teardown () () two functions of the base class. These two functions are actually a template method. When the test is running, setup () will be called for some initialization work. After the test, Teardown () will be called to do some "good work", such as resource recycling, etc. . Of course, you can also override these two functions because they are defined in the base class into an empty method, not a pure virtual function.

In addition, the header file is to be added in the CPP:

#include "plussuite.h"

4, write product code according to test code

After writing the above test code, compile. Compiling affirmation, but the compiler will tell us that the CPLUS class has no statements, because we have not implemented the CPLUS class yet! The current job is to implement the CPLUS class immediately, let the compilation pass. Now you should smell a little "test driver" taste?

Create an MFC EXTENSION DLL in the VC, add class CPLUs in this Project, which is as follows:

Class AFX_EXT_CLASS CPLUS

{

PUBLIC:

CPlus ();

Virtual ~ cplus (); public:

Int Add (int NNum1, int NNUM2);

}

Only one method is the method of our test code to test. Let's see its implementation:

INT CPLUS :: Add (int nnum1, int nnum2)

{

RETURN NNUM1 NNUM2;

}

Figure 6

Click "Browse", as shown in Figure 7:

Figure 7

This should be deeper about the name of the tests we say before. Plus is a test package TestSuite, which contains a test case that includes a test method below.

At this point, we have a detailed introduction to the application of the CPPUnit test framework, hoping to help you in the TDD process.

Reference: CPPUNIT source code and documentation

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

New Post(0)