CPPUnit使用说明
2008-7-29
一.安装
1. 下载:http://cppunit.sourceforge.net
2. 安装
先将压缩包上传到linux上,解压缩后进行安装:
#./configure
#make
#make install
二.CPPUnit使用
1. 需要在make文件中加入-lcppunit(及-ldl),把CPPUnit的库包含进来;
2. main函数(main.cpp文件)的内容为:
#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;
}
这个内容不需要修改,直接COPY就可用使用了。
下面是一个测试类的源文件和头文件
头文件BusiObj_test.h:
#ifndef _BUSIOBJ_TEST_H_
#define _BUSIOBJ_TEST_H_
#include <cppunit/extensions/HelperMacros.h>
class BusiObjTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( BusiObjTest ); //注册我们要测试的类
CPPUNIT_TEST(MyTest); //注册我们要测试的函数
//如果要注册多个函数,在此增加就可以了
CPPUNIT_TEST_SUITE_END();
public:
void setUp(); // 初始化函数
void tearDown(); // 清理函数
void MyTest(); //我们自己增加的测试函数
};
#endif
说明:
1.测试类从TestFixture派生,该类声明了setUp(),tearDown()函数,分别进行重载以用来初始化测试和释放资源
2.在测试类的头文件类声明中使用以下宏进行声明测试套件
CPPUNIT_TESTSUITE(测试类名)
CPPUNIT_TEST(测试函数名1)
CPPUNIT_TEST(测试函数名2)
CPPUNIT_TEST_SUITE_END()
源文件BusiObj_test.cpp的内容:
#include "BusiObj_test.h"
#include "iostream.h"
#include "BusiObj.h"
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( BusiObjTest ); //测试套件注册
void BusiObjTest::setUp()
{
cout << "test begin...." << endl;
}
void BusiObjTest::tearDown()
{
cout << "test end...." << endl;
}
//your first testcase
void BusiObjTest::MyTest()
{
//add your test here
int a=1;
int b=21;
cout << "MyTest() test begin...." << endl;
//assert your test result
CPPUNIT_ASSERT_EQUAL_MESSAGE ("correct response!",a,b);
}
说明:
1.测试套件注册使用以下宏
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(类名,名字空间)
CPPUNIT_TEST_SUITE_REGISTRATION(类名)
以上两个宏在测试类的cpp文件开头添加,第一个宏将测试类注册到“名字空间”里,“名字空间”是一个字符串 第二个宏将测试类注册到缺省名字空间里
2.输出结果
运行成功之后的输出如下:
运行不成功的输出如下:
三. 其他说明
1.CppUnit 提供了多种验证成功失败的方式:
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和tual之间差大于delta时失败,用于浮点数比较
CPPUNIT_ASSERT_THROW(expression, ExceptionType):判断执行表达式expression后是否抛出ExceptionType异常。
CPPUNIT_ASSERT_NO_THROW(expression):断言执行表达式expression后无异常抛出。
2. 网站解析服务器测试用例:
测试用例:
//查看生成的文件格式是否正确
void BusiObjTest::CreativeDetailStatTest2()
{
AdsResolveGetValue stResolve;
char *pResData = "";
long lOrderId=1234;
long lCreativeId=4321;
CBusiObj oBusiObj;
stResolve.init();
strncpy(stResolve.sCliIP,"10.1.75.179",sizeof(stResolve.sCliIP));
strncpy(oBusiObj.stResolverCfg.sLogPath,"./", sizeof(oBusiObj.stResolverCfg.sLogPath));
oBusiObj.stResolverCfg.iRecordsBufferSize = 5;
oBusiObj.stResolverCfg.iTimeOut = 10;
time(&oBusiObj.m_CreativeStartTime);
oBusiObj.stResolverCfg.pstCreativeDetailInfo = (STCreativeDetailInfo *) new char[1024];
CBusiObj BusiObjTest;
oBusiObj.CreativeDetailStat(stResolve, pResData, lOrderId, lCreativeId);
pResData = "Cookie: pvid=6654454083; flv=9.0; sg_source=00001_003_01/r/n";
oBusiObj.CreativeDetailStat(stResolve, pResData, lOrderId, lCreativeId);
sleep(20);
oBusiObj.CreativeDetailStat(stResolve, pResData, lOrderId, lCreativeId);
sleep(20);
delete oBusiObj.stResolverCfg.pstCreativeDetailInfo;
}
WEB服务器的函数代码:
void CBusiObj::CreativeDetailStat(AdsResolveGetValue& p_stResolve, const char * p_pcResData,
long lOrderId, long lCreativeId)
{
}