Linux下单元测试工具Cppunit的简单运用

转载地址:http://blog.csdn.net/abcdef0966/article/details/5699248

一、           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
  1. /********************************************** 
  2. Money.h 
  3.  
  4.  
  5. ***********************************************/  
  6. #ifndef _MONEY_H  
  7. #define _MONEY_H  
  8.  
  9. #include <iostream>  
  10. #include <string>  
  11. using namespace std;  
  12.   
  13. class CMoney  
  14. {  
  15. public:  
  16.   CMoney( double amount, string currency )  
  17.     : m_amount( amount )  
  18.     , m_currency( currency )  
  19.   {  
  20.   }  
  21.   
  22.   ~CMoney(){}  
  23.   
  24.   double GetAmount() const;  
  25.   
  26.   string GetCurrency() const;  
  27.   
  28.   bool operator ==( const CMoney &other ) const;  
  29.   
  30.   bool operator !=( const CMoney &other ) const;  
  31.   
  32.   CMoney &operator +=( const CMoney &other );  
  33.   
  34. private:  
  35.   double m_amount;  
  36.   string m_currency;  
  37. };  
  38.  
  39. #endif  

[c-sharp] view plaincopy
  1. /********************************************* 
  2. Money.cpp 
  3.  
  4. ***********************************************/  
  5. #include <iostream>  
  6. #include <string>  
  7. #include "Money.h"  
  8.   
  9. using namespace std;  
  10.   
  11. double CMoney::GetAmount() const  
  12. {  
  13.   return m_amount;  
  14. }  
  15.   
  16. string CMoney::GetCurrency() const  
  17. {  
  18.   return m_currency;  
  19. }  
  20.   
  21. bool CMoney::operator ==( const CMoney &other ) const  
  22. {  
  23.   return ((m_amount == other.m_amount) &&  
  24.          (m_currency == other.m_currency));  
  25. }  
  26.   
  27. bool CMoney::operator !=( const CMoney &other ) const  
  28. {  
  29.   return !(*this == other);  
  30. }  
  31.   
  32. CMoney &CMoney::operator +=( const CMoney &other )  
  33. {  
  34.   if ( m_currency != other.m_currency )  
  35.       cout <<  "Incompatible moneys" << endl;  
  36.   
  37.   m_amount += other.m_amount;  
  38.   return *this;  
  39. }  

测试工程:~/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
  1. /******************************************* 
  2. Money_Test.h 
  3. 2010.5.28 
  4.  
  5. ********************************************/  
  6. #ifndef _MONEY_TEST_H  
  7. #define _MONEY_TEST_H  
  8.  
  9. #include "cppunit/extensions/HelperMacros.h"  
  10. #include "Money.h"  
  11.   
  12. class CMoneyTest:public CppUnit::TestFixture  
  13. {  
  14.     /*声明一个TestSuite*/  
  15.     CPPUNIT_TEST_SUITE(CMoneyTest);  
  16.   
  17.     /*添加测试用例到TestSuite,定义新的测试用例都要在这里声明; 
  18.       如果此处未声明某个测试用例,程序编译和运行都不会报错 
  19.       仅仅是该测试用例不会被执行。 
  20.      */  
  21.     CPPUNIT_TEST(testConstructor);  
  22.     CPPUNIT_TEST(testOptorEqual);  
  23.     CPPUNIT_TEST(testOptorNotEqual);  
  24.     CPPUNIT_TEST(testOptorAdd);  
  25.   
  26.     /*TestSuite声明完成*/  
  27.     CPPUNIT_TEST_SUITE_END();  
  28.   
  29. public:  
  30.     CMoneyTest(){}  
  31.     /*初始化 */  
  32.     void setUp();  
  33.     /*清除动作 */  
  34.     void tearDown();  
  35.   
  36.     /*test app in Money.cpp*/  
  37.     /*test case */  
  38.     void testConstructor();  
  39.     void testOptorEqual();  
  40.     void testOptorNotEqual();  
  41.     void testOptorAdd();  
  42. };  

 

[c-sharp] view plaincopy
  1. /************************************ 
  2. Money_Test.cpp 
  3. 5.28 
  4.  
  5. ****************************************/  
  6. #include "Money_Test.h"  
  7. #include "Money.h"  
  8. #include <string>  
  9.   
  10. using namespace std;  
  11.   
  12. /* 将该TestSuite注册到名字为“alltest”的TestSuite中,如果未定义会自动定义,也可以使用CPPUNIT_TEST_SUITE_REGISTRATION( MathTest );定义到全局未命名的TestSuite中 */  
  13.   
  14. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CMoneyTest,"alltest");  
  15.   
  16. /*初始化动作*/  
  17. void CMoneyTest::setUp()  
  18. {  
  19.   
  20. }  
  21.   
  22. /*清除动作*/  
  23. void CMoneyTest::tearDown()  
  24. {  
  25.   
  26. }  
  27.   
  28. /*编写测试用例, 
  29. 此处编写四个用例分别来测试CMoney类的四个成员函数*/  
  30. /*test app in Money.cpp*/  
  31.   
  32. /*test constructor*/  
  33. void CMoneyTest::testConstructor()  
  34. {  
  35.     double dNum = 22124.44;  
  36.     string sCurrency = "DD";  
  37.     CMoney MyMoney(dNum, sCurrency);  
  38.   
  39.     CPPUNIT_ASSERT_EQUAL(dNum, MyMoney.GetAmount());  
  40.     CPPUNIT_ASSERT_EQUAL(sCurrency, MyMoney.GetCurrency());  
  41.   
  42. }  
  43.   
  44.   
  45. /*test operator ==()*/  
  46. void CMoneyTest::testOptorEqual()  
  47. {  
  48.     // Set up  
  49.     const CMoney money123FF( 123, "FF" );  
  50.     const CMoney money123USD( 123, "USD" );  
  51.     const CMoney money12FF( 12, "FF" );  
  52.     const CMoney money12USD( 12, "USD" );  
  53.   
  54.     // Process & Check  
  55.     CPPUNIT_ASSERT(money123FF == money123FF);    // ==  
  56.     CPPUNIT_ASSERT(!(money12FF == money123FF));     // != amount  
  57.     CPPUNIT_ASSERT(!(money123USD == money123FF));   // != currency  
  58.     CPPUNIT_ASSERT(!(money12USD == money123FF));     
  59. // != currency and != amount  
  60.   
  61. }  
  62.   
  63. /*test operator!=()*/  
  64. void CMoneyTest::testOptorNotEqual()  
  65. {  
  66.     // Set up  
  67.     const CMoney money123FF( 123, "FF" );  
  68.     const CMoney money123USD( 123, "USD" );  
  69.     const CMoney money12FF( 12, "FF" );  
  70.     const CMoney money12USD( 12, "USD" );  
  71.   
  72.     // Process & Check  
  73.     CPPUNIT_ASSERT(!(money123FF != money123FF));    // ==  
  74.     CPPUNIT_ASSERT(money12FF != money123FF);     // != amount  
  75.     CPPUNIT_ASSERT(money123USD != money123FF);   // != currency  
  76.     CPPUNIT_ASSERT(money12USD != money123FF);      
  77. // != currency and != amount  
  78.   
  79. }  
  80.   
  81. /*test operator+=()*/  
  82. void CMoneyTest::testOptorAdd()  
  83. {  
  84.   // Set up  
  85.   const CMoney money12FF( 12, "FF" );  
  86.   const CMoney expectedMoney( 135, "FF" );  
  87.   
  88.   // Process  
  89.   CMoney money( 123, "FF" );  
  90.   money += money12FF;  
  91.   
  92.   // Check  
  93.   CPPUNIT_ASSERT( expectedMoney ==  money );           // add works  
  94. }  
[c-sharp] view plaincopy
  1. /**************************************************** 
  2. Money_Test_Main.cpp 
  3. 2010.5.28 
  4.  
  5. ********************************************************/  
  6. #include <cppunit/extensions/TestFactoryRegistry.h>  
  7. #include <cppunit/ui/text/TestRunner.h>  
  8.   
  9. int main()  
  10. {  
  11.     CppUnit::TextUi::TestRunner runner;  
  12.   
  13.     /*从注册的TestSuite获取特定的TestSuite, 
  14. 没有参数的话则获取未命名的TestSuite*/  
  15.     CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry("alltest");  
  16.   
  17.     /*添加这个TestSuite到TestRunner中*/  
  18.     runner.addTest(registry.makeTest());  
  19.   
  20.     /*运行测试*/  
  21.     runner.run();  
  22. }  

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使用指南

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值