Linux下Cppunit的简单运用
2015-08-28 11:37
681 查看
操作系统:
软件版本:cppunit-1.12.0.tar.gz
代码调试通过
一、 CppUnit的原理
先简单介绍几个CppUnit的基本术语:
1、Fixture:一个或一组测试用例的测试对象。可以是你要测试的对象或者函数。
2、TestCase:测试用例。是对测试对象的某个功能或流程编写的测试代码。对一个Fixture,可能有多个测试用例。
3、TestSuite:同时运行的测试用例的集合。可以是一个fixture的多个测试函数、也可以是多个fixture的所有测试用例。
使用时,在测试的主文件中将TestCase注册到TestSuite中,并运行。
二、 下载与安装
可以在http://sourceforge.net/projects/cppunit/找到最新的源码并下载至本地。我下载的版本是cppunit-1.12.0.tar.gz
解压:tar -zxvf cppunit-1.12.0.tar.gz
进入cppunit-1.12.0的目录
//因为我是非root用户,没有对/usr/local/lib/和/usr/include的相应权限,//所以将安装的根目录设置为我的个人目录
.configure –prefix=/home/me
make
make install
这样,库文件就复制到/home/me了。
还要将cppunit-1.12.0中的头文件include复制到/home/me中。
三、利用cppunit建立测试的一般框架
通常将测试代码和被测的代码放在不同的工程里面,以免对我们要测试的代码造成污染,这也是运用cppunit的优点之一吧。
这样我们便有两个工程:
其一、待测工程:~/money
有两个文件:
Money.h
Money.cpp
[c-sharp] view plaincopy
/**********************************************
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
[c-sharp] view plaincopy
/*********************************************
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;
}
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);
}
CMoney &CMoney::operator +=( const CMoney &other )
{
if ( m_currency != other.m_currency )
cout << "Incompatible moneys" << endl;
m_amount += other.m_amount;
return *this;
}
测试工程:~/MoneyTest
该工程下有三个文件:
Money_Test.h
Money_Test.cpp
Money_Test_Main.cpp
这三个文件的作用分别是:
Money_Test.h:声明一个TestSuite,并将你所需要的测试用例都在此处进行声明
Mone_Test.cpp:编写测试用例
Money_Test_Main.cpp:运行测试。该文件与具体的测试用例无关。
[c-sharp] view plaincopy
/*******************************************
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
[c-sharp] view plaincopy
/************************************
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()
{
}
/*编写测试用例,
此处编写四个用例分别来测试CMoney类的四个成员函数*/
/*test app in Money.cpp*/
/*test constructor*/
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());
}
/*test operator ==()*/
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)); // != amount
CPPUNIT_ASSERT(!(money123USD == money123FF)); // != currency
CPPUNIT_ASSERT(!(money12USD == money123FF));
// != currency and != amount
}
/*test operator!=()*/
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); // != amount
CPPUNIT_ASSERT(money123USD != money123FF); // != currency
CPPUNIT_ASSERT(money12USD != money123FF);
// != currency and != amount
}
/*test operator+=()*/
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 ); // add works
}
[c-sharp] view plaincopy
/****************************************************
Money_Test_Main.cpp
2010.5.28
********************************************************/
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main()
{
CppUnit::TextUi::TestRunner runner;
/*从注册的TestSuite获取特定的TestSuite,
没有参数的话则获取未命名的TestSuite*/
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
/*添加这个TestSuite到TestRunner中*/
runner.addTest(registry.makeTest());
/*运行测试*/
runner.run();
}
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和actual之间差大于delta时失败
关于CPPUNIT_ASSERT_EQUAL还有一点要说明,该宏对于expected和actual是有要求的,也就是所谓的Requirement:
具有相同的类型(比如都是std::string)
可以使用<<序列化到std::strstream(assertion_traits<T>::toString中指明)
能用==作比较(assertion_traits<T>::equal中指明)
不过,后两条可以通过为assertion_traits定制特化版本去除掉。
四、编译与调试
编译、链接动态库:
g++ -o test Money_Test.cpp Money_Test_Main.cpp ~/money/Money.cpp -I ~/money -I ~/cppunit/include -lcppunit -ldl -L ~/cppunit/lib
运行前要将共享库的目录放到LD_LIBRARY_PATH中。
export LD_LIBRARY_PATH=~/cppunit/lib:$LD_LIBRARY_PATH
运行:
./test
测试结果:
OK (4 tests)
说明所写的4个测试用例均成功。
参考资料
Tx7do@上海半丁 Linux下的CppUnit 的HelloWorld手记
李群:便利的开发工具 CppUnit 快速使用指南
http://www.ibm.com/developerworks/cn/linux/l-cppunit/index.html
CppUnit使用指南
软件版本:cppunit-1.12.0.tar.gz
代码调试通过
一、 CppUnit的原理
先简单介绍几个CppUnit的基本术语:
1、Fixture:一个或一组测试用例的测试对象。可以是你要测试的对象或者函数。
2、TestCase:测试用例。是对测试对象的某个功能或流程编写的测试代码。对一个Fixture,可能有多个测试用例。
3、TestSuite:同时运行的测试用例的集合。可以是一个fixture的多个测试函数、也可以是多个fixture的所有测试用例。
使用时,在测试的主文件中将TestCase注册到TestSuite中,并运行。
二、 下载与安装
可以在http://sourceforge.net/projects/cppunit/找到最新的源码并下载至本地。我下载的版本是cppunit-1.12.0.tar.gz
解压:tar -zxvf cppunit-1.12.0.tar.gz
进入cppunit-1.12.0的目录
//因为我是非root用户,没有对/usr/local/lib/和/usr/include的相应权限,//所以将安装的根目录设置为我的个人目录
.configure –prefix=/home/me
make
make install
这样,库文件就复制到/home/me了。
还要将cppunit-1.12.0中的头文件include复制到/home/me中。
三、利用cppunit建立测试的一般框架
通常将测试代码和被测的代码放在不同的工程里面,以免对我们要测试的代码造成污染,这也是运用cppunit的优点之一吧。
这样我们便有两个工程:
其一、待测工程:~/money
有两个文件:
Money.h
Money.cpp
[c-sharp] view plaincopy
/**********************************************
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
[c-sharp] view plaincopy
/*********************************************
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;
}
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);
}
CMoney &CMoney::operator +=( const CMoney &other )
{
if ( m_currency != other.m_currency )
cout << "Incompatible moneys" << endl;
m_amount += other.m_amount;
return *this;
}
测试工程:~/MoneyTest
该工程下有三个文件:
Money_Test.h
Money_Test.cpp
Money_Test_Main.cpp
这三个文件的作用分别是:
Money_Test.h:声明一个TestSuite,并将你所需要的测试用例都在此处进行声明
Mone_Test.cpp:编写测试用例
Money_Test_Main.cpp:运行测试。该文件与具体的测试用例无关。
[c-sharp] view plaincopy
/*******************************************
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
[c-sharp] view plaincopy
/************************************
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()
{
}
/*编写测试用例,
此处编写四个用例分别来测试CMoney类的四个成员函数*/
/*test app in Money.cpp*/
/*test constructor*/
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());
}
/*test operator ==()*/
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)); // != amount
CPPUNIT_ASSERT(!(money123USD == money123FF)); // != currency
CPPUNIT_ASSERT(!(money12USD == money123FF));
// != currency and != amount
}
/*test operator!=()*/
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); // != amount
CPPUNIT_ASSERT(money123USD != money123FF); // != currency
CPPUNIT_ASSERT(money12USD != money123FF);
// != currency and != amount
}
/*test operator+=()*/
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 ); // add works
}
[c-sharp] view plaincopy
/****************************************************
Money_Test_Main.cpp
2010.5.28
********************************************************/
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main()
{
CppUnit::TextUi::TestRunner runner;
/*从注册的TestSuite获取特定的TestSuite,
没有参数的话则获取未命名的TestSuite*/
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
/*添加这个TestSuite到TestRunner中*/
runner.addTest(registry.makeTest());
/*运行测试*/
runner.run();
}
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和actual之间差大于delta时失败
关于CPPUNIT_ASSERT_EQUAL还有一点要说明,该宏对于expected和actual是有要求的,也就是所谓的Requirement:
具有相同的类型(比如都是std::string)
可以使用<<序列化到std::strstream(assertion_traits<T>::toString中指明)
能用==作比较(assertion_traits<T>::equal中指明)
不过,后两条可以通过为assertion_traits定制特化版本去除掉。
四、编译与调试
编译、链接动态库:
g++ -o test Money_Test.cpp Money_Test_Main.cpp ~/money/Money.cpp -I ~/money -I ~/cppunit/include -lcppunit -ldl -L ~/cppunit/lib
运行前要将共享库的目录放到LD_LIBRARY_PATH中。
export LD_LIBRARY_PATH=~/cppunit/lib:$LD_LIBRARY_PATH
运行:
./test
测试结果:
OK (4 tests)
说明所写的4个测试用例均成功。
参考资料
Tx7do@上海半丁 Linux下的CppUnit 的HelloWorld手记
李群:便利的开发工具 CppUnit 快速使用指南
http://www.ibm.com/developerworks/cn/linux/l-cppunit/index.html
CppUnit使用指南
相关文章推荐
- linux查看目录下占用空间最大的目录
- linux下文件功能描述
- linux使用FIO测试磁盘的iops
- CentOS系统提示用户名不在sudoers文件中
- Centos 上使用Mono+MVC5+WebApi+Sqlite
- Linux设备驱动模型的private_data 简单理解
- LinuxMint
- Linux双网卡绑定实践
- linux下的gdb本地调试及命令解析
- 初学Linux 命令使用小技巧
- linux-centos65-jdk安装-亲测可用
- linux命令大全
- centos虚拟机桥接配置
- linux 进程地址空间的一步步探究
- 总结下常用的linux命令
- centos中修改fd限制
- centos 6.5 lnmp环境部署一些二次修改事项
- DNS从服务器迁移(基于windows为主,linux为从实现同步)
- Linux源代码分析工具链
- 使用Samba实现Linux与Windows文件共享实践