您的位置:软件测试 > 开源软件测试 > 开源单元测试工具 > cppUnit
cppunit在linux下的安装使用
作者:网络转载 发布时间:[ 2013/3/28 13:41:25 ] 推荐标签:

一、系统环境:
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 ....

软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd