Linux下CppUnit的安装与使用

一、系统环境:
Red Hat Enterprise Linux4.0
 Kernel:2.6.9-5 EL
二、Red Hat Enterprise Linux4.0下CppUnit1.12.0的安装
取得:
http://sourceforge.net/projects/cppunit/ 最新的稳定版本为1.12.0。下载:cppunit-1.12.0.tar.gz。
解压缩:
tar -xzf cppunit-1.12.0.tar.gz

生成make file:
./configure

安装:
make
make install

配置共享库:
vi ld.so.conf
添加:/usr/local/lib
保存
可以使用
ldconfig -v | grep cppunit
命令查看是否配置成功。

三、实例

#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);
    }
};

//注册测试用例,很重要,只有在这里注册了才会运行测试方法
CPPUNIT_TEST_SUITE_REGISTRATION( COther_TestCase );

//main方法中的东西都不需要改动,直接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 ....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值