Description of C ++ unit test instructions

Source: Internet
Author: User
Tags xunit

Background: C ++ unit test, Visual C ++ 6.0, and Windows2000. If there is something wrong with this article, please criticize and correct it. Test-driven development is an important part of extreme programming. After reading this article, you will surely have a lot of GAINS. I hope this article will teach you more.

XUnit, a test framework based on test-driven development, provides a convenient tool for us to use test-driven development during the development process, so that we can quickly perform unit testing. There are many XUnit members, such as JUnit and PythonUnit. CppUnit is a member of the XUnit family. It is a test framework specially designed for C ++.

This article does not give a detailed introduction to the CppUnit source code, but just introduces the CppUnit application. In this article, you will see:
1. Components of CppUnit source code.
2. How to set up your development environment to use CppUnit.
3. How to add the test code to your product code should, in turn, add the product code to the test code. In TDD, there are first test code and then product code), and the test is carried out through CppUnit.

I. CppUnit Source Code Composition
The source code for the CppUnit testing framework can be downloaded from the After downloading and unzipping, the main folders are: doc: CppUnit instructions. In addition, there are three instruction documents in the Code root directory, which are respectively install,install-unix,install-win32.txt. Examples: The example provided by CpppUnit is also a test of CppUnit itself, through which you can learn how to use the CppUnit testing framework for development.

Ii. Initial Knowledge of CppUnit test environment
After extracting the source code package, you must take a look at what CppUnit is like? OK. Let's unveil the secrets of CppUnit:

1. Enter the example folder and use VC to open examples. dsw. Let's take a look at the test examples provided by CppUnit. These examples are all unit test sets for CppUnit itself. On the one hand, this is the test cases written during the CppUnit framework developed by CppUnit authors, and on the other hand, we can use these examples to learn how to add test cases in our own projects.

2. Set the CppUnitTestApp project to Active ProjectWin32 Debug.) After compilation and running, you can see the GUI of CppUnit for unit test TestRunner. Click "Run ",
This is a unit test result for CppUnit. It indicates that we have passed 11 tests just now. CppUnit indicates all unit tests in the tree structure. In CppUnit, the smallest test unit is called the TestMethod test method, and multiple related test methods can form a TestCase test case. Multiple test cases form the TestSuite test package. Test packages are nested together to form the tree structure we see above. We can choose any of the Tree nodes for unit testing.

3. Set the CppUnitTestMain project to Active ProjectWin32 Debug), compile and run, and let's look at another unit test environment. This is a text-based unit test environment. CppUnit provides several testing environments, one based on text and the other based on GUI.

4. Set the HostApp project to Active ProjectWin32 Debug) and compile and run the program. This is also a test of CppUnit itself, but it shows us various failed tests. In a GUI-based test environment, if the test fails, the progress bar is displayed in red, and vice versa. From the test results, we can see the name of the failed unit test, the reason for the test failure, and the file and number of lines of the statement where the test fails.

3. C ++ unit test settings understand the CppUnit test environment. You must be ready to feel the feeling of test-driven development in your development process. However, you need to set up your development environment before using CppUnit.

1. lib and dll of CppUnit
CppUnit provides two sets of framework libraries: static lib and dynamic dll. In development, we can make a choice based on the actual situation. Go to the src folder and open CppUnitLibraries. dsw. Compile the two projects respectively, and the output location is the lib folder.

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

  • List the compiling environments supported by the C ++ Compiler
  • I will introduce you to the C ++ programming Portal
  • How C ++ compiler can handle exceptions
  • Introduction to C ++ programming skills
  • This section describes how to use C ++ and how to modify errors.

To facilitate development, we copy the compiled lib and dll, including the Debug and Release versions, to a folder created by ourselves (you can also skip this ), for example, F: \ cppunit1.9.0 \ lib \. We also copy the include folder in the CppUnit source code to our own include folder. Set the include path and lib path in tools/options/directories/include files and library files of VC. Finally, do not forget to link the correct lib in your project.

2. Enable the rtti switch in your c ++ unit test. The specific location is Project Settings/C ++ Language.

3. Set environment variables for TestRunner. dll
TestRunner. dll provides a GUI-based testing environment. In order for our test program to call it correctly, TestRunner. dll must be located in the path of your test program. However, the simplest method is to add the TestRunner. dll Path in the environment variable Path of the operating system, which is the most convenient.

Related Article

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: 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.