#apt-cache search cppunit
libcppunit-1.12-1 - Unit Testing Library for C++
libcppunit-dev - Unit Testing Library for C++
libcppunit-doc - Unit Testing Library for C++
libcppunit-subunit-dev - SubunitTestProgressListener for CPPUnit - Development headers
libcppunit-subunit0 - SubunitTestProgressListener for CPPUnit - C++ shared library
libcunit1 - Unit Testing Library for C
libcunit1-dev - Unit Testing Library for C -- development files
libcunit1-doc - Unit Testing Library for C -- documentation
libcunit1-ncurses - Unit Testing Library for C (ncurses)
libcunit1-ncurses-dev - Unit Testing Library for C (ncurses) -- development files
libqxcppunit-dev - A Qt4-based GUI for running tests with CppUnit - development files
libqxcppunitd1 - A Qt4-based GUI for running tests with CppUnit
#sudo apt-get install libcppunit-doc libcppunit-dev
#ls /usr/include/cppunit
#ls /usr/lib/libcppunit.so
#ls /usr/share/doc/libcppunit-doc
changelog.Debian.gz copyright examples html README.Debian
#firefox /usr/share/doc/libcppunit-doc/html/index.html &
#cp /usr/share/doc/libcppunit-doc/examples ~/cppunit/
#cd ~/cppunit/examples
#sudp apt-get install libtool automake
#sh setup.sh
#./configure
#make check
#./cppunittest/cppunittestmain
#cd ~/myexamples
#ls
main.cpp myBasicMath.cpp myBasicMath.h testMyBasicMath.cpp testMyBasicMath.h
In myBasicMath.h
1 #ifndef MYBASICMATH_H
2 #define MYBASICMATH_H
3
4 class MyBasicMath
5 {
6 public:
7 int Addition(int x, int y);
8 int Multiply(int x, int y);
9 };
10
11 #endif
In myBasicMath.cpp
1 #include "myBasicMath.h"
2
3 int MyBasicMath::Addition(int x, int y)
4 {
5 return (x + y);
6 }
7
8 int MyBasicMath::Multiply(int x, int y)
9 {
10 return (x * y);
11 }
In testMyBasicMath.h
1 #ifndef TESTMYBASICMATH_H
2 #define TESTMYBASICMATH_H
3
4 #include <cppunit/TestFixture.h>
5 #include <cppunit/extensions/HelperMacros.h>
6 #include "myBasicMath.h"
7
8 class TestMyBasicMath : public CppUnit::TestFixture
9 {
10 CPPUNIT_TEST_SUITE(TestMyBasicMath);
11 CPPUNIT_TEST(testAddition);
12 CPPUNIT_TEST(testMultiply);
13 CPPUNIT_TEST_SUITE_END();
14
15 public:
16 void setUp(void);
17 void tearDown(void);
18
19 protected:
20 void testAddition(void);
21 void testMultiply(void);
22
23 private:
24
25 MyBasicMath *mTestObj;
26 };
27
28 #endif
In testMyBasicMath.cpp
1 #include "testMyBasicMath.h"
2
3 CPPUNIT_TEST_SUITE_REGISTRATION( TestMyBasicMath );
4
5 void TestMyBasicMath::setUp(void)
6 {
7 mTestObj = new MyBasicMath();
8 }
9
10 void TestMyBasicMath::tearDown(void)
11 {
12 delete mTestObj;
13 }
14
15 void TestMyBasicMath::testAddition(void)
16 {
17 CPPUNIT_ASSERT(5 == mTestObj->Addition(2,3));
18 }
19
20 void TestMyBasicMath::testMultiply(void)
21 {
22 CPPUNIT_ASSERT(6 == mTestObj->Multiply(2,3));
23 }
24
In main.cpp
1 #include <cppunit/TestResult.h>
2 #include <cppunit/TestResultCollector.h>
3 #include <cppunit/TestRunner.h>
4 #include <cppunit/BriefTestProgressListener.h>
5 #include <cppunit/CompilerOutputter.h>
6 #include <cppunit/XmlOutputter.h>
7 #include "testMyBasicMath.h"
8
9 using namespace CppUnit;
10 using namespace CPPUNIT_NS;
11 using namespace std;
12
13 int main(int argc, char* argv[])
14 {
15 // informs test-listener about testresults
16 TestResult testresult;
17
18 // register listener for collecting the test-results
19 TestResultCollector collectedresults;
20 testresult.addListener (&collectedresults);
21
22 // register listener for per-test progress output
23 BriefTestProgressListener progress;
24 testresult.addListener (&progress);
25
26 // insert test-suite at test-runner by registry
27 TestRunner testrunner;
28 testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
29 testrunner.run(testresult);
30
31 // output results in compiler-format
32 CompilerOutputter compileroutputter(&collectedresults, CPPUNIT_NS::stdCOut());
33 compileroutputter.write ();
34
35 // Output XML for Jenkins CPPunit plugin
36 ofstream xmlFileOut("cppTestMyBasicMathResults.xml");
37 XmlOutputter xmlOut(&collectedresults, xmlFileOut);
38 xmlOut.write();
39
40 // return 0 if tests were successful
41 return collectedresults.wasSuccessful() ? 0 : 1;
42 }
#g++ -o testMyBasicMath myBasicMath.cpp testMyBasicMath.cpp main.cpp -lcppunit
#./testMyBasicMath
TestMyBasicMath::testAddition : OK
TestMyBasicMath::testMultiply : OK
OK (2)
CPPUNIT_ASSERT(condition) // 确信condition为真
CPPUNIT_ASSERT_MESSAGE(message, condition)
// 当condition为假时失败, 并打印message
CPPUNIT_FAIL(message)
// 当前测试失败, 并打印message
CPPUNIT_ASSERT_EQUAL(expected, actual)
// 确信两者相等
CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)
// 失败的同时打印message
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
// 当expected和actual之间差大于delta时失败
libcppunit-1.12-1 - Unit Testing Library for C++
libcppunit-dev - Unit Testing Library for C++
libcppunit-doc - Unit Testing Library for C++
libcppunit-subunit-dev - SubunitTestProgressListener for CPPUnit - Development headers
libcppunit-subunit0 - SubunitTestProgressListener for CPPUnit - C++ shared library
libcunit1 - Unit Testing Library for C
libcunit1-dev - Unit Testing Library for C -- development files
libcunit1-doc - Unit Testing Library for C -- documentation
libcunit1-ncurses - Unit Testing Library for C (ncurses)
libcunit1-ncurses-dev - Unit Testing Library for C (ncurses) -- development files
libqxcppunit-dev - A Qt4-based GUI for running tests with CppUnit - development files
libqxcppunitd1 - A Qt4-based GUI for running tests with CppUnit
#sudo apt-get install libcppunit-doc libcppunit-dev
#ls /usr/include/cppunit
#ls /usr/lib/libcppunit.so
#ls /usr/share/doc/libcppunit-doc
changelog.Debian.gz copyright examples html README.Debian
#firefox /usr/share/doc/libcppunit-doc/html/index.html &
#cp /usr/share/doc/libcppunit-doc/examples ~/cppunit/
#cd ~/cppunit/examples
#sudp apt-get install libtool automake
#sh setup.sh
#./configure
#make check
#./cppunittest/cppunittestmain
#cd ~/myexamples
#ls
main.cpp myBasicMath.cpp myBasicMath.h testMyBasicMath.cpp testMyBasicMath.h
In myBasicMath.h
1 #ifndef MYBASICMATH_H
2 #define MYBASICMATH_H
3
4 class MyBasicMath
5 {
6 public:
7 int Addition(int x, int y);
8 int Multiply(int x, int y);
9 };
10
11 #endif
In myBasicMath.cpp
1 #include "myBasicMath.h"
2
3 int MyBasicMath::Addition(int x, int y)
4 {
5 return (x + y);
6 }
7
8 int MyBasicMath::Multiply(int x, int y)
9 {
10 return (x * y);
11 }
In testMyBasicMath.h
1 #ifndef TESTMYBASICMATH_H
2 #define TESTMYBASICMATH_H
3
4 #include <cppunit/TestFixture.h>
5 #include <cppunit/extensions/HelperMacros.h>
6 #include "myBasicMath.h"
7
8 class TestMyBasicMath : public CppUnit::TestFixture
9 {
10 CPPUNIT_TEST_SUITE(TestMyBasicMath);
11 CPPUNIT_TEST(testAddition);
12 CPPUNIT_TEST(testMultiply);
13 CPPUNIT_TEST_SUITE_END();
14
15 public:
16 void setUp(void);
17 void tearDown(void);
18
19 protected:
20 void testAddition(void);
21 void testMultiply(void);
22
23 private:
24
25 MyBasicMath *mTestObj;
26 };
27
28 #endif
In testMyBasicMath.cpp
1 #include "testMyBasicMath.h"
2
3 CPPUNIT_TEST_SUITE_REGISTRATION( TestMyBasicMath );
4
5 void TestMyBasicMath::setUp(void)
6 {
7 mTestObj = new MyBasicMath();
8 }
9
10 void TestMyBasicMath::tearDown(void)
11 {
12 delete mTestObj;
13 }
14
15 void TestMyBasicMath::testAddition(void)
16 {
17 CPPUNIT_ASSERT(5 == mTestObj->Addition(2,3));
18 }
19
20 void TestMyBasicMath::testMultiply(void)
21 {
22 CPPUNIT_ASSERT(6 == mTestObj->Multiply(2,3));
23 }
24
In main.cpp
1 #include <cppunit/TestResult.h>
2 #include <cppunit/TestResultCollector.h>
3 #include <cppunit/TestRunner.h>
4 #include <cppunit/BriefTestProgressListener.h>
5 #include <cppunit/CompilerOutputter.h>
6 #include <cppunit/XmlOutputter.h>
7 #include "testMyBasicMath.h"
8
9 using namespace CppUnit;
10 using namespace CPPUNIT_NS;
11 using namespace std;
12
13 int main(int argc, char* argv[])
14 {
15 // informs test-listener about testresults
16 TestResult testresult;
17
18 // register listener for collecting the test-results
19 TestResultCollector collectedresults;
20 testresult.addListener (&collectedresults);
21
22 // register listener for per-test progress output
23 BriefTestProgressListener progress;
24 testresult.addListener (&progress);
25
26 // insert test-suite at test-runner by registry
27 TestRunner testrunner;
28 testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
29 testrunner.run(testresult);
30
31 // output results in compiler-format
32 CompilerOutputter compileroutputter(&collectedresults, CPPUNIT_NS::stdCOut());
33 compileroutputter.write ();
34
35 // Output XML for Jenkins CPPunit plugin
36 ofstream xmlFileOut("cppTestMyBasicMathResults.xml");
37 XmlOutputter xmlOut(&collectedresults, xmlFileOut);
38 xmlOut.write();
39
40 // return 0 if tests were successful
41 return collectedresults.wasSuccessful() ? 0 : 1;
42 }
#g++ -o testMyBasicMath myBasicMath.cpp testMyBasicMath.cpp main.cpp -lcppunit
#./testMyBasicMath
TestMyBasicMath::testAddition : OK
TestMyBasicMath::testMultiply : OK
OK (2)
CPPUNIT_ASSERT(condition) // 确信condition为真
CPPUNIT_ASSERT_MESSAGE(message, condition)
// 当condition为假时失败, 并打印message
CPPUNIT_FAIL(message)
// 当前测试失败, 并打印message
CPPUNIT_ASSERT_EQUAL(expected, actual)
// 确信两者相等
CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual)
// 失败的同时打印message
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta)
// 当expected和actual之间差大于delta时失败