Cppunit testing framework (1 ))

Source: Internet
Author: User
Tags xunit
Author: cpluser
Web: http://tdd.nease.net
Email: cpluser@hotmail.com
Blog: http://blog.csdn.net/cpluser/

DemoCodeDownload

Test-driven development (TDD) is the center of the development process. It insists that you write the test code based on the product code before writing the actual code. The goal of the development process is to first pass the test and then optimize the design structure. Test-driven development is an important part of extreme programming. 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. cppunitSource code.
2. How to set up your development environment to use cppunit.
3. How to add the test code for your product code (in fact, you should add the product code to the test code in turn. In TDD, there are first test code and then product code), and the test is carried out through cppunit.

The background of this article is cppunit1.9.0, Visual C ++ 6.0, and Windows2000. If the description is incorrect, please criticize and correct it.
 
I. cppunit Source Code Composition
The source code for the cppunit testing framework can be downloaded from the http://sourceforge.net/projects/cppunit. After downloading and decompressing the package, you will see the following folder:

Figure 1

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.
Include:Cppunit header file.
SRC:Cppunit source code directory.
 
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 project (Win32 Debug), compile and run it, and you can see the GUI of cppunit for unit test testrunner. Click "run". The page shown in Figure 2 is displayed:

Figure 2

This is a unit test result for cppunit. It indicates that we have passed 11 tests just now.
Click "Browse". You can also select the unit test you want to perform. 3:

Figure 3

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 project (Win32 Debug), compile and run it, and let's look at another unit test environment. 4:

Figure 4

This is a text-based unit test environment. Cppunit provides several test environments, one based on text and the other based on GUI, that is, figure 3.
 
4. Set the hostapp project to active project (Win32 Debug) and compile and run it. 5:

Figure 5

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.

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.