Cppunitlite Usage Details

Source: Internet
Author: User

I always wanted to perform a unit test on C ++ for my current project, and I also wanted to see the implementation of a unit test framework. We accidentally discovered that cppunitlite and cppunit are lightweight and easy to use.

1. Download and directory structure
(Http://download.csdn.net/source/304011), the downloaded directory structure is as follows:
/OM
/Cppunitlite
/Failure. cpp
/Failure. h
/Simplestring. cpp
/Simplestring. h
...
/Cppunittests
/CPP
/Stackmain. cpp
/Stacktest. cpp
/Stack. h
/Readme.txt

Several failure under the cppunitlite root directory. h, simplestring. CPP is the core code of cppunitlite, and runtime uses stack as an example to teach us how to apply cppunitlite to our project (of course, you can also directly open cppunittests under the cppunittests folder. DSW, the running result should be consistent with the following steps ):

Ii. Quick Start
1. first, build a C ++ static library project cppunitlite. CPP and other items (that is, the core code mentioned above) are all added to the project and compiled into cppunitlite. lib to remove the pre-compilation option.

2. Create a Win32 console project for the stack, add the stackmain. cpp and stacktest. cpp files, statically link cppunitlite. Lib, and set the project
Dependency: the stack project depends on the cppunitlite project.

3. Run it. If all the preceding settings are correct, there were no test failures will be displayed in the console.

3. Use cppunitlite for your project.

How can we use the cppunitlite test framework for our project? Specifically, what files and code should we add? Let's take a look at stackmain. cpp and stacktest. cpp.

Stackmain. cpp:

  1. # Include "cppunitlite/testharness. H"
  2. Int main ()
  3. {
  4. Testresult TR;
  5. Testregistry: runalltests (TR );
  6. Return 0;
  7. }

Stacktest. cpp:

  1. # Include "cppunitlite/testharness. H"
  2. # Include "Stack. H"
  3. # Include <string>
  4. Simplestring stringfrom (const STD: string & value)
  5. {
  6. Return simplestring (value. c_str ());
  7. }
  8. Test (stack, creation)
  9. {
  10. Stack S;
  11. Longs_equal (0, S. Size ());
  12. STD: String B = "asa ";
  13. Check_equal ("asa", B );
  14. }

Stackmain. cpp is our main function, which ensures that all testcase is run, and almost no change is required.
Stacktest. cpp is the code we want to write. What did we do?
Add the test framework header file "testharness. H ".
Add the header file "Stack. H" of the class you want to test"
Add macro test. The code after this macro is what you want to test. For example, if we want to test 1 + 1 = 2, we can write it like this:

  1. Test (Foo, creation)
  2. {
  3. Int A = 1;
  4. Int B = 1;
  5. Int result = A + B;
  6. Check (result = 2 );
  7. Check (result = 3 );
  8. }

If yes, the following message is displayed: Failure: "1 = 2" line 16 in C:/Documents and Settings/jczeng/desktop/cppunit.pdf
E/OM/stacktest. cpp
There were 1 failures
Press any key to continue
It will prompt a few cases that have not passed and which line is wrong.

4. Use cppunitlite in MFC.
The above are just the simplest cases listed above. If you are testing projects such as MFC, pay attention to the following points:
1. For the console project, select support for MFC.
2. Because MFC supports multi-thread debugging, change the output fprintf (stdout, XXX) in cppunitlite to printf.
3. In addition, there may be some Link errors. For specific questions, check msdn and discuss them.

CPPUnitliteImplementation Analysis

1. The testregistry class is a singleton mode.

L an external instance is obtained through the instance () method.

L it is actually the registry of all test cases.

L it contains a pointer to the test case linked list, that is, the test pointer. And maintain the linked list.

L The instance method add adds a new test case to the linked list and uses the existing test list as the next node of the new test case. Therefore, the test case linked list is a LIFO queue.

L it is responsible for calling the execution methods of all test cases

 

2. Class test is the base class of all test cases.

L constructor is responsible for adding itself to the testregistry registry and serving as the first node.

L it provides a pure virtual function run, the specific test case override this virtual method.

L it contains a pointer to execute the next test case, so test itself is a test case linked list.

 

3. Macro test contains the actual code of a test case.

L defines a subclass of test.

L The pure virtual function run of the override base class, so that the subclass can not be instantiated.

L generates an instance of this subclass. (The constructor of the base class ensures that the instance is added to the testregistry registry ).

 

4. The testresult class is responsible for collecting the execution results of all test cases.

L The virtual function teststarted indicates that the test case is started.

L The virtual function testended indicates that the test case has been executed successfully.

L virtual function addfailure indicates that an error occurs when a test is executed.

 

5. When the secondary macro check and check_equal do not match the expected result, the virtual function addfailure of testresult is called.

 

6. The failure and simplestring classes belong to the auxiliary classes, which indicate the error information (such as the file name, row number, expected results, and actual results) during the test, and are simple strings.

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.