/// MathTest.h
// A TestFixture subclass.
// Announce: use as your owner risk.
// Author : liqun (liqun@nsfocus.com)
// Data : 2003-7-5
#include "cppunit/TestFixture.h"
class MathTest : public CppUnit::TestFixture {
protected:
int m_value1, m_value2;
public:
MathTest() {}
// 初始化函数
void setUp ();
// 清理函数
void tearDown();
// 测试加法的测试函数
void testAdd ();
// 可以添加新的测试函数
};
/// MathTest.cpp
// A TestFixture subclass.
// Announce: use as your owner risk.
// Author : liqun (liqun@nsfocus.com)
// Data : 2003-7-5
#include "MathTest.h"
#include "cppunit/TestAssert.h"
void MathTest::setUp()
{
m_value1 = 2;
m_value2 = 3;
}
void MathTest::tearDown()
{
}
void MathTest::testAdd()
{
int result = m_value1 + m_value2;
CPPUNIT_ASSERT( result == 5 );
}
然后编写 main 函数,把需要测试的测试用例组织到 TestSuite 中,然后通过 TestRuner 运行。这部分代码后期添加新的测试用例时需要改动的不多。只需要把新的测试用例添加到 TestSuite 中即可。
/// main.cpp
// Main file for cppunit test.
// Announce: use as your owner risk.
// Author : liqun (liqun@nsfocus.com)
// Data : 2003-7-5
// Note : Cannot compile, only for study.
#include "MathTest.h"
#include "cppunit/ui/text/TestRunner.h"
#include "cppunit/TestCaller.h"
#include "cppunit/TestSuite.h"
int main()
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestSuite *suite= new CppUnit::TestSuite();