[单元測试]_[VC2010使用gtest单元測试入门]


场景:

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,在文件夹srcmsvc下有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:workspacescript-test	est_gtest	est_gtestDebug>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:workspacescript-test	est_gtest	est_gtest	est_gtest	est_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

原文地址:https://www.cnblogs.com/yangykaifa/p/6835475.html