Cppunit is similar to JUnit. The basic principle is the same, but I think it is a little more troublesome to configure it than JUnit.
(The configuration of cppunit in vc6.0 and vc.net will be introduced in the next article)
Take a simple money class as an example.
// Name: Money. h
# Ifndef money_h
# Define money_h
# Include <string>
Class money
{
Public:
Money (double amount)
: M_amount (amount)
{
}
Double getamount () const
{
Return m_amount;
}
PRIVATE:
Double m_amount;
};
# Endif
We now need to use cppunit to test the two functions ----- constructor and getamount () function.
Step 1: Of course, the cppunit environment is configured. I will not talk about it here (the environment described below is in VC)
Step 2: Use the Wizard to create a console application named moneyapp. Edit the project's moneyapp. cpp as follows:
// Moneyapp. cpp: defines the entry point for the console application.
//
# Include "stdafx. H"
# Include <cppunit/compileroutputter. h>
# Include <cppunit/extensions/testfactoryregistry. h>
# Include <cppunit/UI/text/testrunner. h>
Int main (INT argc, char * argv [])
{
// Get the top level suite from the Registry
Cppunit: test * suite = cppunit: testfactoryregistry: getregistry (). maketest ();
// Adds the test to the list of test to run
Cppunit: textui: testrunner runner;
Runner. addtest (suite );
// Change the default outputter to a compiler error format outputter
Runner. setoutputter (New cppunit: compileroutputter (& runner. Result (),
STD: cerr ));
// Run the tests.
Bool wassucessful = runner. Run ();
// Return Error Code 1 if the one of test failed.
Return wassucessful? 0: 1;
}
Step 3: now it is time for us to write and test for the money class. First, we create a new header file named moneytest. H. Its content is as follows:
# Ifndef moneytest_h
# Define moneytest_h
# Include <cppunit/extensions/helpermacros. h>
Class moneytest: Public cppunit: testfixture
{
// Add the project to be tested here
Cppunit_test_suite (moneytest );
Cppunit_test (testgetamount );
Cppunit_test_suite_end ();
Public:
Void setup ();
Void teardown ();
Void testconstructor ();
Void testgetamount ();
};
# Endif // moneytest_h
Then, create a new CPP file named moneytest. cpp. (By the way, the real test is done here.) The content of the file is as follows:
# Include "stdafx. H"
# Include "moneytest. H"
# Include "Money. H"
// This sentence is very important. It should be compared with the previous "cppunit: test * suite = cppunit: testfactoryregistry: getregistry (). maketest ()".
// Registers the fixture into the 'registry'
Cppunit_test_suite_registration (moneytest );
Void
Moneytest: setup ()
{
}
Void
Moneytest: teardown ()
{
}
Void
Moneytest: testconstructor ()
{
}
Void
Moneytest: testgetamount ()
{
Const double longnumber = 12345678.90123;
Money money (longnumber );
Cppunit_assert_equal (longnumber, money. getamount ());
}
Step 4: OK. You can press Ctrl + F5 to run it. The running result is as follows:
.
OK (1)
Press any key to continue
If you want to see the effect of assertion failure, you can do this:
Set cppunit_assert_equal (longnumber, money. getamount ());
Change to cppunit_assert_equal (100, money. getamount ());
The running result is as follows.
. F
D:/zhangyd/moneyapp/moneytest. cpp (38): assertion
Test name: moneytest: testgetamount
Equality assertion failed
-Expected: 100
-Actual: 1.23457e + 007
Failures !!!
Run: 1 failure total: 1 failures: 1 errors: 0
Press any key to continue