一、系統(tǒng)環(huán)境:
Red Hat Enterprise Linux4.0
Kernel:2.6.9-5 EL
二、Red Hat Enterprise Linux4.0下CppUnit1.12.0的安裝
取得:
http://sourceforge.net/projects/cppunit/新的穩(wěn)定版本為1.12.0。下載:CppUnit-1.12.0.tar.gz。
解壓縮:
tar -xzf CppUnit-1.12.0.tar.gz
生成make file:
./configure
安裝:
make
make install
配置共享庫(kù):
vi ld.so.conf
添加:/usr/local/lib
保存
可以使用
ldconfig -v | grep CppUnit
命令查看是否配置成功。
三、實(shí)例
#include <CppUnit/extensions/HelperMacros.h>
#include <CppUnit/BriefTestProgressListener.h>
#include <CppUnit/CompilerOutputter.h>
#include <CppUnit/extensions/TestFactoryRegistry.h>
#include <CppUnit/TestResult.h>
#include <CppUnit/TestResultCollector.h>
#include <CppUnit/TestRunner.h>
#include "ldap_util.h"
#include "sequence_operator.h"
//test COther_Operator
class COther_TestCase : public CppUnit_NS::TestFixture
{
CppUnit_TEST_SUITE( COther_TestCase );
CppUnit_TEST( CSequence_Operator_testGetNextSeq );
CppUnit_TEST( testBackupRestore );
CppUnit_TEST_SUITE_END();
protected:
LDAP *ld;
CSequence_Operator *base;
public:
void setUp()
{
CLDAP_Util::GetConnection(&ld);
base = new CSequence_Operator();
}
void tearDown()
{
CLDAP_Util::CloseConnection(&ld);
delete base;
}
protected:
void CSequence_Operator_testGetNextSeq()
{
int seq,nextseq;
CppUnit_ASSERT_NO_THROW(seq = base->GetNextSeq(ld,1));
CppUnit_ASSERT_NO_THROW(nextseq = base->GetNextSeq(ld,1));
CppUnit_ASSERT_EQUAL(seq+1,nextseq);
}
void testBackupRestore()
{
CDiscoveryDomain_Operator dd_operator;
CDiscoveryDomainSet_Operator dds_operator;
vector<ISNS_ATTR> keyAttr_dd;
vector<ISNS_ATTR> addAttr_dd;
vector<ISNS_ATTR> keyAttr_dds;
vector<ISNS_ATTR> addAttr_dds;
ISNS_ATTR temp;
temp.tag = 2065;
temp.len = 1;
temp.val.dd_id = 0;
keyAttr_dd.push_back(temp);
addAttr_dd.push_back(temp);
temp.tag = 2066;
temp.len = 16;
strcpy(temp.val.dd_sym_name,"DD Symbolic Name");
addAttr_dd.push_back(temp);
temp.tag = 2049;
temp.len = 1;
temp.val.dd_id = 0;
keyAttr_dds.push_back(temp);
addAttr_dds.push_back(temp);
temp.tag = 2050;
temp.len = 1;
strcpy(temp.val.dds_sym_name,"DDS Name");
addAttr_dds.push_back(temp);
//insert dd
CppUnit_ASSERT_NO_THROW(dd_operator.Insert(ld,keyAttr_dd,addAttr_dd));
//backup db
CppUnit_ASSERT_EQUAL(CLDAP_Util::Backup(),LDAP_SUCCESS);
//remove dd
CppUnit_ASSERT_NO_THROW(dd_operator.Remove(ld,keyAttr_dd));
//insert dds
CppUnit_ASSERT_NO_THROW(dds_operator.Insert(ld,keyAttr_dds,addAttr_dds));
//restore db
CppUnit_ASSERT_EQUAL(CLDAP_Util::Restore(),LDAP_SUCCESS);
//check dd exist
vector<ISNS_ATTR> rstAttr;
CppUnit_ASSERT_NO_THROW(rstAttr = dd_operator.Search(ld,keyAttr_dd,keyAttr_dd));
int size = rstAttr.size();
CppUnit_ASSERT_EQUAL(size,1);
//remove dd
CppUnit_ASSERT_NO_THROW(dd_operator.Remove(ld,keyAttr_dd));
//check dds exist
CppUnit_ASSERT_NO_THROW(rstAttr = dds_operator.Search(ld,keyAttr_dds,addAttr_dds));
size = rstAttr.size();
CppUnit_ASSERT_EQUAL(size,0);
}
};
//注冊(cè)測(cè)試用例,很重要,只有在這里注冊(cè)了才會(huì)運(yùn)行測(cè)試方法
CppUnit_TEST_SUITE_REGISTRATION( COther_TestCase );
//main方法中的東西都不需要改動(dòng),直接copy可以了。
int main( int argc, char* argv[] )
{
// Create the event manager and test controller
CppUnit_NS::TestResult controller;
// Add a listener that colllects test result
CppUnit_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CppUnit_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CppUnit_NS::TestRunner runner;
runner.addTest( CppUnit_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
// Print test in a compiler compatible format.
CppUnit_NS::CompilerOutputter outputter( &result, CppUnit_NS::stdCOut() );
outputter.write();
return result.wasSuccessful() ? 0 : 1;
}
四、編譯
g++ -ggdb testcase.cpp -o testcase.o
g++ -lstdc++ -lCppUnit -f -0 testcase.o ....