这篇文章主要是介绍简洁并且强大的
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 阅读(4273)
评论(3) 编辑 收藏 引用 所属分类:
C++