Cppunit Study Notes

Source: Internet
Author: User

Cppunit is a C ++ program testing framework converted from JUnit, first written by Michael feathers
Codeunittestfirst is not a testing technique, it's a design technique.

First, we need to understand the Implementation ideas of the JUnit/xunit framework.
Typical unit tests can be described as follows:
Make sure that the method accepts input within the expected range and returns the expected results for each test input.

Unit test: unit test is an independent unit of work. In Java applications, an independent unit of work often refers to a method (but not always ). For comparison, integration testing and acceptance testing checks how multiple components interact. A work unit is a task that does not depend on the completion of any other task.

Unit testing usually focuses on whether a method meets the API contract. Just as people agree to exchange contracts for specific goods or services under certain conditions, API contracts are seen as formal agreements for method interfaces. The method requires the caller to provide a specific value or object, and (as an exchange) will return a specific value or object. If the contract cannot be met, an exception is thrown to indicate that the contract is not observed. If the behavior of a method is inconsistent with expectation, we say this method damages the contract.

API contract: A View of the application programming interface (API). It is regarded as a formal agreement between the caller and the called. Unit Tests often help define API contracts by validating the expected results. The statement of the API contract comes from the design by contract practice (http://archive.eiffel.com/doc/manuals/technology/contract) that is popular with the Eiffel programming language ).

What content is worth testing right-bicep
Right -- are the results right? The result is correct.
B -- are all the boundary conditions correct? Are all boundary conditions correct?
I -- can you check inverse relationships? Whether reverse link is checked
C -- can you cross-check results using other means? Check whether the cross-checking result is correct. (check whether the results are consistent using another algorithm) e -- can you force error conditions to happen? Indicates whether the result is correct when a mandatory error condition occurs.
P -- are performance characteristics within bounds? Meet performance requirements?

Correct Boundary Condition
C -- conformance: Does the value conform to an expected format?
O -- ordering: is the set of values ordered or unordered as appropriate?
R -- range: Is the value within reasonable minimum and maximum values?
R -- Reference: Does the code reference anything external that isn't under direct control of the Code itself?
E -- existence: Does the value exist (e.g., is non-null, nonzero, present in a set, etc .)?
C -- cardinality: Are there exactly enough values?
T -- time (absolute and relative): Is everything happening in order? At the right time? In time?

In general, the following situations are prone to bugs.
-- Totally bogus or inconsistent input values, such as a file name "! * W: Xn & GI/W> g/h # WQ @".
-- Badly formatted data, such as an e-mail address without a top-level domain ("Fred @ foobar .").
-- Empty or missing values (such as 0, 0: 0, "", or null ).
-- Values far in excess of reasonable expectations, such as a person's age of 10,000 years.
-- Duplicates in lists that shouldn't have duplicates.
-- Ordered lists that aren't, and vice-versa. try handing a pre-sorted list to a sort algorithm, for instance -- or even a reverse-sorted list.
-- Things that arrive out of order, or happen out of expected order, such as trying to print a document before logging in, for instance.

-- Useful links:
Http://sourceforge.net/projects/cppunit/
Http://cppunit.sourceforge.net/cppunit-wiki/FrontPage
Http://www.junit.org/
Http://www.mockobjects.com/

-- Installation on Linux
1. Download cppunit-1.10.2 package from cppunit site and extract it
Http://cppunit.sourceforge.net

2. Download Autoconf 2.2.4
Http://ftp.man.poznan.pl/pub/gnu/gnu/autoconf/

3. Install Autoconf
./Configure
./Make
./Make install

4. Install cppunit
./Configure cxx =/usr/bin/g ++ 296 cxxflags =-O2
./Make
./Make install
./Make clean

5. Verify Installation
Ls/usr/local/lib/libcppunit *

-- Installation on Windows
(Summarized by hongshengyi)
1. Compile the cppunit directory/src/cppunit/cppunitlibraries. DSW cppunit_dll project, generate cppunit_dll.lib and cppunit_dll.dll in release, and generate cppunitd_dll.lib and cppunitd_dll.dll. This is the basic cppunit class library.

2. Compile the testrunner project in the cppunit directory/src/cppunit/cppunitlibraries. DSW. Release generates testrunner. lib and testrunner. dll. debug generates testrunnerd. lib and testrunnerd. dll. This is a class library using the visual interface of MFC.

3. Place all lib files under the commonfiles/lib directory.
Or choose tool-> select-> directory-> library files in VC to put the lib directory in cppunit.

4. Place the DLL file under the corresponding debug and release executable directories.

5. Choose tool-> select-> directory-> include files in VC to include the include directory in cppunit.

6. In VC, go to project-> Settings-> link to put cppunitd_dll.lib
Select enable rtti on the projects/settings.../C ++ language page.
On the projects/settings.../C ++/code generation page, select the content in the use run-time Library:
For release, select "mulithreaded DLL ".
For debug, select "Debug multihreaded DLL ".

7. You can introduce the macro addingunittestmethod. DSM to conveniently generate a test framework.

8. After writing the class to be tested and tested, write a main function.

-- Example
-------------------- Hello world of CPP unit ------------------
Http://pantras.free.fr/articles/helloworld.html)

# Include <iostream>

# Include <cppunit/testrunner. h>
# Include <cppunit/testresult. h>
# Include <cppunit/testresultcollector. h>
# Include <cppunit/extensions/helpermacros. h>
# Include <cppunit/brieftestprogresslistener. h>
# Include <cppunit/extensions/testfactoryregistry. h>

Class test: Public cppunit_ns: testcase
{
Cppunit_test_suite (test );
Cppunit_test (testhelloworld );
Cppunit_test_suite_end ();

Public:
Void setup (void ){}
Void teardown (void ){}

Protected:
Void testhelloworld (void) {STD: cout <"Hello, world! "<STD: Endl ;}
};

Cppunit_test_suite_registration (test );

Int main (INT argc, char ** argv)
{
// 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 );

Return result. wassuccessful ()? 0: 1;
}
Complie:
/Usr/bin/g +++ 296-O2-g-o helloworld. CPP/
-I.-I./h-I/usr/include-I/usr/local/include/
-L/usr/lib-L/usr/local/lib-LDL-lm-lpthread-lcppunit

-- Key points:
1. First write the test code and then write the code that complies with the test. After at least some code is completed, the corresponding test code is completed;
2. The test code does not need to cover all the details, but should have corresponding test cases for all main functions and possible errors;
3. If a bug is found, write the corresponding test case and debug it;
4. Summarize the causes of bugs and write test cases for other codes;
5. Complete the code every time, run all the previous test cases, verify the impact on the previous code, and eliminate this impact as soon as possible;
6. Maintain the test code continuously to ensure that all tests are passed after the code changes;

-- Main steps
1. initialize test fixture
2. initialize testcase: Setup
3. Execute test case: assert
4. Release testcase: teardown
5. Release Test Fixture

Provided assertions:
Cppunit_assert (condition) // make sure the condition is true.
Cppunit_assert_message (message, condition) // if the condition is false, it fails and the message is printed.
Cppunit_fail (Message) // the current test fails and the message is printed.
Cppunit_assert_equal (expected, actual) // make sure the two are equal
Cppunit_assert_cmd_message (message, expected, actual) // print the message when the request fails.
Cppunit_assert_doubles_equal (expected, actual, Delta) // failure when the difference between expected and actual is greater than Delta

Running Mode:
Cpunit: textui: testrunner // testrunner in text mode
Cppunit: qtui: testrunner // testrunner in QT Mode
Cppunit: mfcui: testrunner // testrunner In the MFC Mode

 

(Thanks Liqun and hongshengyi's 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: 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.