CPP unit usage

Source: Internet
Author: User
Preparations

Download the cppunit source code on sourceforge.net at: http://sourceforge.net/projects/cppunit, now the latest version: cppunit-1.12.0.tar.gz

Ii. Reference

1) Check the source code of cppunit. Under the src directory under the directory you downloaded, there is samples under the examples directory.
2) There is a TXT help file in the source code project: INSTALL-WIN32.txt, very good, but it is in English.
3) for more help, see:
// Http://www.vckbase.com/document/viewdoc? Id = 1762
// Http://www.codeproject.com/library/Using_CPPUnit.asp
// Http://cppunit.sourceforge.net/doc/1.8.0/cppunit_cookbook.html#cppunit_cookbook

Three compilations (I use vs to compile)

Cppunit: core framework library of unit test
Cppunit_dll: The function is the same as above. The difference is that the above is static Lib, which is a dynamic DLL. You can select one of them for use.
Testrunner: MFC extended DLL, responsible for reporting GUI browsing

Testpluginrunner: The plug-in DLL used to run the interface specified by cppunit
Dllplugintester: Used in the post event of plugin DLL when running in plugin mode, and the result is checked during compilation of plugin DLL.
Dsplugin: The addin required by vc6. If some cases fail, double-click it to jump to the specified code line. This is not required in vs200 *.

Example 4 (only macro definition is used for implementation. It is too simple to use macro definition. For other principles and class implementation and relationships, refer to the above link)

1) Suppose we want to test the class:

// Test Data
Class sampleclass
{
Public:
Int add (int I, Int J)
{
Return I + J;
}
Int square (int I)
{
Return I * I;
}
};

2) We need to implement the test case: (this class inherits cppunit: testfixture or cppunit: testcase, and can implement two virtual functions setup () and teardown (), they automatically call each test function before and after case.) Class sampleclass_testcase: Public cppunit: testfixture
{
Cppunit_test_suite (sampleclass_testcase );
Cppunit_test (add_test );
Cppunit_test (square_test );
Cppunit_test_suite_end ();

Public:
Void setup ()
{
Testclass = new sampleclass ();
}

Void teardown ()
{
Delete testclass;
Testclass = NULL;
}

Protected:

Void add_test ()
{
Cppunit_assert (6 = testclass-> Add (1, 4 ));
}

Void square_test ()
{
Cppunit_assert (10 = testclass-> Square (3 ));
}

Public:
Static STD: String getsuitename ();

PRIVATE:
Sampleclass * testclass;
};

STD: String sampleclass_testcase: getsuitename ()
{
Return "testsample ";
}

3) Add the above testcase to test suite. Of course, the name of test suite can be defined by yourself. // Cppunit_test_suite_registration (sampleclasstestcase );
Cppunit_test_suite_named_registration (sampleclass_testcase, sampleclass_testcase: getsuitename ());

4) add test suite to test runner, and testrunner is responsible for managing all test suite. You can use cppunit_registry_add (which, to) to establish the tree relationship of the suite. For example: cppunit_registry_add (a, B), cppunit_registry_add (B, c)

Cppunit_registry_add_to_default (sampleclass_testcase: getsuitename ());

5) Call testrunner to display the result. There are three methods:
First: In GUI mode, we need to create an mfc exe and add the following code to call the following function in our theapp: initinstance, and the function must be changed to return true:

// 1) Run UI for output in MFC dialog project(.exe)

Void testmain ()
{

// Declare a test runner, fill it with our registered tests and run them
Cppunit: mfcui: testrunner runner;
Runner. addtest (cppunit_ns: testfactoryregistry: getregistry (). maketest ());

Runner. Run (); // show UI

}

The second method is console. You need to create a console EXE, add the following code, and call the following function in main: // 2) Run console for output in console project(.exe)

Void testmain ()
{
// Create the event manager and test Controller
Cppunit_ns: testresult controller;

// Add a listener that colllects Test Result
Cppunit_ns: testresultcollector result;
Controller. addlistener (& result );

// Add a listener that print dots as test run.
Cppunit_ns: brieftestprogresslistener progress;
Controller. addlistener (& progress );

// Add the top suite to the test runner
Cppunit_ns: testrunner runner;
// Runner. addtest (cppunit_ns: testfactoryregistry: getregistry (). maketest ());
Runner. Run (Controller );

// Print test in a compiler compatible format.
Cppunit_ns: compileroutputter outputter (& result, cppunit_ns: stdcout ());
Outputter. Write ();
}


Third: plugin, create a DLL, add the following code, and must call one of the following Macros in this dll: (Note that your DLL itself has a main function)
We can add a call to dllplugintesterud.exe in the Post-eventevent of this DLL to get the result during compilation, or open the compiled DLL using testpluginrunner and view the result in Gui mode. /// Implements all the plug-in stuffs, winmain
Cppunit_plugin_implement ();
/// Or
/// Only export function not has main function. If you have main, use below macro
// Cppunit_plugin_exported_function_impl (cppunit_ns: testplugindefaultimpl );

Four more

1) General header files: # include <cppunit/brieftestprogresslistener. h>
# Include <cppunit/compileroutputter. h>
# Include <cppunit/testresult. h>
# Include <cppunit/testresultcollector. h>
# Include <cppunit/testrunner. h>
# Include <cppunit/extensions/testfactoryregistry. h>
# Include <cppunit/UI/mfc/testrunner. h>

2) General macro definition: // cppunit_assert (condition): Checks condition and throws an exception if it's false.
// Cppunit_assert_message (message, condition): Checks condition and throws an exception and showing
// Specified message if it is false.
// Cppunit_assert_equal (expected, current): checks if expected is the same as current, and raises exception
// Showing expected and current values.
// Cppunit_assert_cmd_message (message, expected, current): checks if expected is the same as actual, and
// Raises exception showing expected and current values, and specified message.
// Cppunit_assert_doubles_equal (expected, current, Delta): checks if expected and current difference is
// Smaller than Delta. If it fails, expected and current values are shown.

3) if we want to perform unit tests on the functions in our entire project and want to separate the test code from our official code, in this case, we can create a separate DLL (using plugin) or EXE (output to Gui or console) for our test code, and add our formal code to this test project, but we do not copy our formal Code. However, if the static lib of our project is called, we can directly call the code without adding it to the test module.

5 Reference
Cppunit project hompage: http://sourceforge.net/projects/cppunit/
How to Use cppunit for unit testing: http://www.vckbase.com/document/viewdoc? Id = 1762
 

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.