????CppUnit -- ???C++??????????????????????????????????JUnit?????????????????????е???????????
????????????http://sourceforge.net/apps/mediawiki/cppunit/index.php?title=Main_Page
????1. ???
??????linux?o???????????????????????????????????Eclipse CDT???????????? cppunit ?????????  link library
????2. ???????????????????????м?????????

 

#include <iostream>
//????CppUnit???????
#include "cppunit/TestCase.h"
#include "cppunit/TestResult.h"
#include "cppunit/TestResultCollector.h"
#include "cppunit/TestCaller.h"
#include "cppunit/TextOutputter.h"
using namespace std;
//????????????????
class SimpleCalculator {
public:
int add(int a?? int b) {
return a + b;
}
int sub(int a?? int b) {
return a - b;
}
int mul(int a?? int b) {
return a * b;
}
int div(int a?? int b) {
return a / b;
}
};
//?????????TestCase??
class SimpleCalculatorTest : public CPPUNIT_NS::TestCase {
public:
//????????????
virtual void setUp() {
pSc = new SimpleCalculator();
}
//????????????????
virtual void tearDown() {
delete pSc;
}
//???????
void testAdd() {
CPPUNIT_ASSERT_EQUAL(pSc->add(5??6)?? 11);
}
void testDiv() {
CPPUNIT_ASSERT_EQUAL(pSc->div(10??1)?? 0);
}
private:
SimpleCalculator *pSc;
};
int main() {
CPPUNIT_NS::TestResult tr;
CPPUNIT_NS::TestResultCollector resultCollector;
tr.addListener(&resultCollector);
//testAdd
CPPUNIT_NS::TestCaller<SimpleCalculatorTest> tcAdd("testAdd"?? &SimpleCalculatorTest::testAdd);
tcAdd.run(&tr);
//testDiv
CPPUNIT_NS::TestCaller<SimpleCalculatorTest> tcDiv("testDiv"?? &SimpleCalculatorTest::testDiv);
tcDiv.run(&tr);
CPPUNIT_NS::TextOutputter output(&resultCollector?? std::cout);
output.write();
return 0;
}
/*
CPPUNIT_ASSERT_EQUAL(pSc->div(1??10)?? 0);
OK (2 tests)
CPPUNIT_ASSERT_EQUAL(pSc->div(10??1)?? 0);
!!!FAILURES!!!
Test Results:
Run:  2   Failures: 1   Errors: 0
1) test: testDiv (F) line: 54 ../src/TestCppUnit3.cpp
equality assertion failed
- Expected: 10
- Actual  : 0
*/