CPP单元测试

【1】cppUnit安装
http://cppunit.sourceforge.net/
https://www.freedesktop.org/wiki/Software/cppunit/


安装步骤:
tar xvzf cppunit-1.13.2.tar.gz 
cd cppunit


./configure --prefix /home/longhui/program_cppunit
make
make check
make install


【2】cppUnit安装验证

【3】cppUnit理论框架
http://www.ibm.com/developerworks/cn/linux/l-cppunit/
在 CppUnit 中,一个或一组测试用例的测试对象被称为 Fixture(设施,下文为方便理解尽量使用英文名称)。Fixture 就是被测试的目标,可能是一个对象或者一组相关的对象,甚至一个函数。
有了被测试的 fixture,就可以对这个 fixture 的某个功能、某个可能出错的流程编写测试代码,这样对某个方面完整的测试被称为TestCase(测试用例)。通常写一个 TestCase 的步骤包括:
对 fixture 进行初始化,及其他初始化操作,比如:生成一组被测试的对象,初始化值;
按照要测试的某个功能或者某个流程对 fixture 进行操作;
验证结果是否正确;
对 fixture 的及其他的资源释放等清理工作。

对 fixture 的多个测试用例,通常(1)(4)部分代码都是相似的,CppUnit 在很多地方引入了 setUp 和 tearDown 虚函数。可以在 setUp 函数里完成(1)初始化代码,而在 tearDown 函数中完成(4)代码。具体测试用例函数中只需要完成(2)(3)部分代码即可,运行时 CppUnit 会自动为每个测试用例函数运行 setUp,之后运行 tearDown,这样测试用例之间就没有交叉影响。

工程举例

【1】Money_Test_Main.cpp

/****************************************************
http://blog.csdn.net/abcdef0966/article/details/5699248
Linux下Cppunit的简单运用
********************************************************/
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main()
{
    CppUnit::TextUi::TestRunner runner;

    /*从注册的TestSuite获取特定的TestSuite,没有参数的话则获取未命名的TestSuite*/
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("abcdef");
    /*添加这个TestSuite到TestRunner中*/
    runner.addTest(registry.makeTest());
    /*运行测试*/
    runner.run();
}
【2】Makefile
########################################################
##Standard Makefile for single CPP file
##Version:1.0   Date:20150921   Author:David Long 
##参考链接http://blog.csdn.net/abcdef0966/article/details/5699248
#######################################################

#编译环境
CC = g++

#默认包含本目录的include目录作为头文件搜索路径 CUnit的头文件和库文件的路径
INC	= -I/home/longhui/program_cppunit/include
LIB = -L/home/longhui/program_cppunit/lib

#生成的目标可执行文件名
Target=test

#源文件
MainFile=Money_Test_Main.cpp Money_Test.cpp Money.cpp

$(Target):$(MainFile)
	$(CC) $(MainFile) -o $(Target) $(INC) $(LIB) -lcppunit -static

.PRONY:clean
clean:
	@echo "Removing linked and compiled files......"
	rm -f *~ *.o *.xml $(Target)

【3】Money.cpp

/*********************************************
Money.cpp

***********************************************/
#include <iostream>
#include <string>
#include "Money.h"

using namespace std;

double CMoney::GetAmount() const
{
  return m_amount;
}

string CMoney::GetCurrency() const
{
  return m_currency;
}

/* 重载操作符==:判断两个类是否相等,即CMoney的两个成员都相同才相等 */
bool CMoney::operator ==( const CMoney &other ) const
{
  return ((m_amount == other.m_amount) && (m_currency == other.m_currency));
}

/* 重载操作符!=:判断两个类不相等,==的相反 */
bool CMoney::operator !=( const CMoney &other ) const
{
  return !(*this == other);
}

/* 重载操作符+=:将两个类中的m_amount成员加起来 */
CMoney &CMoney::operator +=( const CMoney &other )
{
  if ( m_currency != other.m_currency )
      cout <<  "Incompatible moneys" << endl;

  m_amount += other.m_amount;
  return *this;
}



【4】Money.h

/**********************************************
Money.h


***********************************************/
#ifndef _MONEY_H
#define _MONEY_H

#include <iostream>
#include <string>
using namespace std;

class CMoney
{
public:
  CMoney( double amount, string currency )
    : m_amount( amount )
    , m_currency( currency )
  {
  }

  ~CMoney(){}

  double GetAmount() const;

  string GetCurrency() const;

  bool operator ==( const CMoney &other ) const;

  bool operator !=( const CMoney &other ) const;

  CMoney &operator +=( const CMoney &other );

private:
  double m_amount;
  string m_currency;
};

#endif



【5】Money_Test.cpp

/************************************
Money_Test.cpp
5.28

****************************************/
#include "Money_Test.h"
#include "Money.h"
#include <string>

using namespace std;

/* 将该TestSuite注册到名字为“alltest”的TestSuite中,
如果未定义会自动定义,也可以使用CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );
定义到全局未命名的TestSuite中 */
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CMoneyTest,"alltest");

/*初始化动作*/
void CMoneyTest::setUp()
{

}

/*清除动作*/
void CMoneyTest::tearDown()
{

}

/* 测试用例1:测试CMoney类的GetAmount成员函数和GetCurrency成员函数 */
void CMoneyTest::testConstructor()
{
    double dNum = 22124.44;
    string sCurrency = "DD";
    CMoney MyMoney(dNum, sCurrency);

    CPPUNIT_ASSERT_EQUAL(dNum, MyMoney.GetAmount());
    CPPUNIT_ASSERT_EQUAL(sCurrency, MyMoney.GetCurrency());

}


/* 测试用例2:测试CMoney类的重载操作符== */
void CMoneyTest::testOptorEqual()
{
    // Set up
    const CMoney money123FF( 123, "FF" );
    const CMoney money123USD( 123, "USD" );
    const CMoney money12FF( 12, "FF" );
    const CMoney money12USD( 12, "USD" );

    // Process & Check
    CPPUNIT_ASSERT(money123FF == money123FF);    
    CPPUNIT_ASSERT(!(money12FF == money123FF));     
    CPPUNIT_ASSERT(!(money123USD == money123FF));  
    CPPUNIT_ASSERT(!(money12USD == money123FF));   

}

/* 测试用例3:测试CMoney类的重载操作符!= */
void CMoneyTest::testOptorNotEqual()
{
    // Set up
    const CMoney money123FF( 123, "FF" );
    const CMoney money123USD( 123, "USD" );
    const CMoney money12FF( 12, "FF" );
    const CMoney money12USD( 12, "USD" );

    // Process & Check
    CPPUNIT_ASSERT(!(money123FF != money123FF)); 
    CPPUNIT_ASSERT(money12FF != money123FF);    
    CPPUNIT_ASSERT(money123USD != money123FF);
    CPPUNIT_ASSERT(money12USD != money123FF);    
}

/* 测试用例4:测试CMoney类的重载操作符+= */
void CMoneyTest::testOptorAdd()
{
  // Set up
  const CMoney money12FF( 12, "FF" );
  const CMoney expectedMoney( 135, "FF" );

  // Process
  CMoney money( 123, "FF" );
  money += money12FF;

  // Check
  CPPUNIT_ASSERT( expectedMoney ==  money );
}


【6】Money_Test.h

/*******************************************
Money_Test.h
2010.5.28

********************************************/
#ifndef _MONEY_TEST_H
#define _MONEY_TEST_H

#include "cppunit/extensions/HelperMacros.h"
#include "Money.h"

class CMoneyTest:public CppUnit::TestFixture
{
    /*声明一个TestSuite*/
    CPPUNIT_TEST_SUITE(CMoneyTest);

    /*添加测试用例到TestSuite,定义新的测试用例都要在这里声明;
      如果此处未声明某个测试用例,程序编译和运行都不会报错
      仅仅是该测试用例不会被执行。
     */
    CPPUNIT_TEST(testConstructor);
    CPPUNIT_TEST(testOptorEqual);
    CPPUNIT_TEST(testOptorNotEqual);
    CPPUNIT_TEST(testOptorAdd);

    /*TestSuite声明完成*/
    CPPUNIT_TEST_SUITE_END();

public:
    CMoneyTest(){}
    /*初始化 */
    void setUp();
    /*清除动作 */
    void tearDown();

    /*test app in Money.cpp*/
    /*test case */
    void testConstructor();
    void testOptorEqual();
    void testOptorNotEqual();
    void testOptorAdd();
};

#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值