My understanding of cppunit

Source: Internet
Author: User
I have some understanding of cppunit (I have never had time, I forgot a lot of things, and I will sort them out later ):
I. Test Cases
1. cppunit is usually used for interface testing. If you perform path testing, you need to consider some special issues because you may need to access the protection of the tested class or private members. In this case, you can consider using the helper class, which derives from the tested class and declares the test class as a friend.
2. The test class is derived from testfixture. This class declares the setup () and teardown () functions, which are reloaded to initialize the test and release resources respectively.
3. Use the following macro to declare the test suite in the header file class declaration of the test class
Cppunit_testsuite (test class name)
Cppunit_test (test function name 1)
Cppunit_test (test function name 2)
Cppunit_test_suite_end ()

The above code prepares the test function. The test function is prototype: void testx ()
4. Test Suite registration using the following macros
Cppunit_test_suite_named_registration (Class Name, namespace)
Cppunit_test_suite_registration (class name)
The preceding two macros are added at the beginning of the CPP file of the test class. The first macro registers the test class to the "namespace". The "namespace" is a string.
The second macro registers the test class to the default namespace.
5. Implementation of CPP. The following example is the implementation of a test function. The path test is completed. ctest_cbackupsource is derived from testfixture.
Void ctest_cbackupsource: testmakesourcename ()
{
Cmybackupsource backupsource;

Cppunit_assert (backupsource. m_strsourcename = "");

Backupsource. setsourcepath ("C: // test. tt ");
Cppunit_assert (backupsource. m_strsourcename = "test. tt ");

Backupsource. setsourcepath ("C: // test //");
Cppunit_assert (backupsource. m_strsourcename = "");

Backupsource. setsourcepath ("test ");
Cppunit_assert (backupsource. m_strsourcename = "test ");
}
Ii. Run Test Cases
// Return the reference of the Test Factory object. The default parameter is "all tests", which corresponds to the test class registered in the default space. during future testing, test cases registered in the default space will be run
Cppunit_ns: testfactoryregistry istry = cppunit_ns: testfactoryregistry: getregistry ();

// After a namespace is added, the test cases registered in the namespace will be run.
Registry. addregistry ("cbackupsource ");
// Registry. addregistry ("cversioninfo ");
// Registry. addregistry ("cshfile ");

// Obtain the test suite, including all the test cases registered in the selected test Space
Cppunit_ns: test * suite = registry. maketest ();

Cppunit_ns: textui: testrunner runner;

Runner. addtest (suite );

// Set the output. Note that the object is released by the runner.
Runner. setoutputter (New cppunit_ns: compileroutputter (& runner. Result (), STD: cerr ));

// Add a listener. eventmanager returns the testresult Class Object and maintains a listener set. Listener determines the test process performance, including the log output format.
Cppunit_ns: brieftestprogresslistener progress;

Runner. eventmanager (). addlistener (& progress );

Nretcode = runner. Run ();

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.