這篇文章主要是介紹簡潔并且強大的
google test 測試框架。
在歷經數月的論戰之后,"
0 bug事件" 告訴我們,"0 bug"是不存在的,那種"0 bug 態度" 和 "0 bug 方法" 更是有bug的。
于是我們需要一種工具來幫助我們更好地進行測試,盡早發現bug,然后修正它。我們不能保證它是"0 bug",至少我們可以讓它足夠好.。
C/C++測試框架有很多:
CPPUnit ,
Boost.Test ,
CppUnitLite ,
NanoCppUnit ,
Unit++ ,
CxxTest ,
Google Test, ... 這些框架我沒有都用過,所以不好做評價。
不過
這里有一篇文章對各種C/C++測試框架進行了綜合評價,有興趣的同學可以參考。
在使用了CPPUnit和GoogleTest之后,覺得GoogleTest更滿足我的需要。下面是一個簡單的GoogleTest使用示例,未接觸過GoogleTest的同學可以從中對GoogleTest有一個最基本的認識。
1
#include <string>
2
#include <algorithm>
3
4
#include "gtest/gtest.h"
5
//! @brief 去除字符串中的重復字符
6
std::string& UniqueString( std::string &refString )
7

{
8
std::sort( refString.begin() , refString.end() );
9
refString.erase( std::unique( refString.begin() , refString.end() ) , refString.end() );
10
11
return refString;
12
}
13
14
// test 1
15
TEST( UniqueString , StringWithDuplicate )
16

{
17
std::string strText( "abcdcba" );
18
EXPECT_EQ( std::string( "abcd" ) , UniqueString( strText ) );
19
}
20
21
// test 2
22
TEST( UniqueString , StringWithoutDuplicate )
23

{
24
std::string strText( "abcd" );
25
EXPECT_EQ( std::string( "abcd" ) , UniqueString( strText ) );
26
}
27
28
29
int main( int argc , char *argv[] )
30

{
31
testing::InitGoogleTest( &argc , argv );
32
return RUN_ALL_TESTS();
33
}
輸出結果如下:

上面只是google test最基本的使用,更多的使用方法可以參考
google test wiki,或者使用下面基于google test wiki 的CHM文件。
點擊下載
posted on 2010-05-26 20:47
luckycat 閱讀(4283)
評論(3) 編輯 收藏 引用 所屬分類:
C++