场景:

1. gtest作为C++的单元測试工具非常优秀了,它集成了非常多标准assert所没有的功能,比方让流程继续运行的EXPECT,仅仅測试特定測试用例的--gtest_filter,

输出xml文件的測试报告.

2.方便的FilePath对路径操作的类和Message设置Log级别,当然还有非常多方便的功能,比方环境变量设置.

using ::testing::internal::FilePath;
using ::testing::Message;

3.这里编写了一个vc2010简单的使用gtest的样例.使用gcc版本号请看,用的Makefile:

http://blog.csdn.net/infoworld/article/details/20457353

代码:

// test_gtest.cpp : 定义控制台应用程序的入口点。
//
// 在 googletest 中实现单元測试,可通过 ASSERT_* 和 EXPECT_*
// 断言来对程序执行结果进行检查。 ASSERT_* 版本号的断言失败时会产生致命失败,
// 并结束当前函数; EXPECT_* 版本号的断言失败时产生非致命失败,但不会中止当前函数。 // 因此, ASSERT_* 经常被用于兴许測试逻辑强制依赖的处理结果的断言,
// 如创建对象后检查指针是否为空,若为空,则兴许对象方法调用会失败;
// 而 EXPECT_* 则用于即使失败也不会影响兴许測试逻辑的处理结果的断言,
// 如某个方法返回结果的多个属性的检查。 #include "stdafx.h"
#include <iostream>
#include "gtest/gtest.h"
using ::testing::internal::FilePath;
using ::testing::Message; using namespace std;
//To generate the XML report, set the GTEST_OUTPUT environment variable or
//the --gtest_output flag to the string "xml:_path_to_output_file_",
//which will create the file at the given location.
//You can also just use the string "xml", in which case the output can be found in the test_detail.xml file in the current directory.
int main(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv); FilePath path(argv[0]);
FilePath currentDir = path.GetCurrentDir();
Message message("currentDir is: ");
message << currentDir.ToString();
GTEST_LOG_(INFO) << message ;
GTEST_LOG_(INFO) << ".............." ; RUN_ALL_TESTS();
system("pause");
return 0;
} TEST(test_main,TestBaseAssert)
{
cout << "TestBaseAssert" << endl;
ASSERT_TRUE(__LINE__);
EXPECT_TRUE(__LINE__); EXPECT_FALSE(true);
ASSERT_FALSE(false);
} TEST(test_main,TestBinaryAssert)
{
cout << "TestBinaryAssert" << endl;
const char* str = "11"; ASSERT_EQ(str,str);
EXPECT_EQ(false,0); ASSERT_NE(str,str+1);
EXPECT_NE(false,1); ASSERT_LT(str,str+1);
EXPECT_LT(0x1,0x2); ASSERT_LE(0x1,0x2);
EXPECT_LE(0x1,0x2); ASSERT_GT(0x2,0x1);
EXPECT_GT(0x2,0x1); ASSERT_GE(0x2,0x1);
EXPECT_GE(0x2,0x1);
} TEST(test_main,TestStrAssert)
{
cout << "TestStrAssert" << endl;
const char* str = "11ab";
std::string str2("11ab");
std::string str21("11aB");
//1.推断字符串是否相等.
ASSERT_STREQ(str,str2.c_str());
EXPECT_STREQ(str,str2.c_str()); //2.字符串大写和小写敏感
ASSERT_STRNE(str,str21.c_str());
EXPECT_STRNE(str,str21.c_str()); //3.字符串大写和小写不敏感
ASSERT_STRCASEEQ(str,str21.c_str());
EXPECT_STRCASEEQ(str,str21.c_str()); str21.append("!");
ASSERT_STRCASENE(str,str21.c_str());
EXPECT_STRCASENE(str,str21.c_str());
}

注意:

1. 使用gtest须要自己先编译,下载,我用的是1.5.0. 完整源文件放在csdn download里,下边看下载链接。

http://code.google.com/p/googletest

2. 假设须要自己编译gtest,在文件夹src\msvc下有sln文件, 默认编译出来的是静态库,貌似动态库使用上有问题.

3. 使用gtest时可能会报错

1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref@facet@locale@std@@QAEPAV123@XZ) 已经在 gtestd.lib(gtest.obj) 中定义
由于C/C++->代码生成->设置了"多线程调试 (/MTd)"
而项目使用的是 多线程调试 DLL (/MDd). 解决的方法:把測试项目改为 多线程调试 (/MTd) 就可以,注意一点,假设把gtest编译为 多线程调试 DLL (/MDd),按CTRL+F5会在
testing::InitGoogleTest(&argc, argv); 崩溃.我预计是共享执行时库DLL的做了某些加锁操作造成的.所以还是把測试项目exe改为
多线程调试 (/MTd)好点.

3.样例输出:

2.输出
C:\workspace\script-test\test_gtest\test_gtest\Debug>test_gtest --gtest_output=x
ml
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from test_main
[ RUN ] test_main.TestBaseAssert
TestBaseAssert
c:\workspace\script-test\test_gtest\test_gtest\test_gtest\test_gtest.cpp(35): er
ror: Value of: true
Actual: true
Expected: false
[ FAILED ] test_main.TestBaseAssert (16 ms)
[ RUN ] test_main.TestBinaryAssert
TestBinaryAssert
[ OK ] test_main.TestBinaryAssert (16 ms)
[ RUN ] test_main.TestStrAssert
TestStrAssert
[ OK ] test_main.TestStrAssert (0 ms)
[----------] 3 tests from test_main (32 ms total) [----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (32 ms total)
[ PASSED ] 2 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] test_main.TestBaseAssert 1 FAILED TEST
请按随意键继续. . .

4.完整项目下载地址,项目里包括了编译好的gtestd.lib库和头文件,偷懒的童鞋能够直接先用着。

docs文件夹里有gtest的一些资料.

http://download.csdn.net/download/infoworld/7539997

最新文章

  1. svn:cleanup failed previous operation has not finished; run cleanup if it was interrupted
  2. 原生JS封装简单动画效果
  3. 自定义评分器Similarity,提高搜索体验(转)
  4. Android Error:Execution failed for task &#39;:app:mergeDebugResources&#39;. &gt; Crunching Cruncher bg_btn.9.png
  5. Data truncation: Truncated incorrect DOUBLE value 解决方案
  6. [转]Jquery Ajax用法
  7. Fast Intro To Java Programming (1)
  8. oracle SQLserver 函数
  9. 洛谷1440 求m区间内的最小值
  10. 最全java的读写操作(转载)
  11. C++的构造函数总结
  12. android armeabi与armeabi-v7a
  13. C++ 中mallon动态分配内存大小用法
  14. Hadoop1.0.3环境搭建流程
  15. Java集合中的HashMap类
  16. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU特性介绍(3)- 命名规则
  17. python之str字符串
  18. 【Math for ML】线性代数之——向量空间
  19. android中sharedPreferences的用法(转)
  20. babun安装,整合到cmder

热门文章

  1. 【Luogu】P3356火星探险问题(费用流)
  2. NOJ——1508火烧赤壁2(并查集+启发式合并+逆序加边)
  3. poj 1981 Circle and Points
  4. elementary os 0.4.1下编译GCC-7.1源码并安装成功
  5. wpf LookUpEdit PopupContentTemplate
  6. Purpose of XMLString::transcode
  7. vue 权限控制按钮3种样式、内容、以及跳转事件
  8. KeyStore和TrustStore
  9. ML | PCA
  10. Oracle服务扫描工具Oscanner