VS2017使用GoogleTest

1. 下载googletest

地址: https://github.com/google/googletest
可以点击tags, 选择release版本

2. 编译googletest

打开msvc文件夹中的gtest.sln
选择Debug/Release进行编译生成

3. 使用gtest

  • 新建空工程
  • 项目->属性->VC++目录, 添加库目录, googletest/include
  • 项目->属性->C/C++->代码生成
    Debug: 运行库改为”多线程调试
    Release: 运行库改为”多线程
  • 项目->属性-> 链接器-> 附加库目录, msvc中的Debug和Release生成目录

4. 测试sample

导入sample: googletest/samples

代码示例:

#include 
#include "gtest/gtest.h"

#ifdef _DEBUG  
#pragma comment(lib, "gtestd.lib")  
#pragma comment(lib, "gtest_maind.lib")  
#else  
#pragma comment(lib, "gtest.lib")  
#pragma comment(lib, "gtest_main.lib")   
#endif

int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();

    getchar();
    return 0;
}


5.不导入lib,直接用源码

添加googletest/src中的除了gtest-all.c, gtest_main.c的其他.c, .h:
VS2017使用GoogleTest_第1张图片

以下代码就不用了:

#ifdef _DEBUG

#pragma comment(lib, "gtestd.lib")

#pragma comment(lib, "gtest_maind.lib")

#else

#pragma comment(lib, "gtest.lib")

#pragma comment(lib, "gtest_main.lib")

#endif


运行示例:
VS2017使用GoogleTest_第2张图片

你可能感兴趣的:(Windows)