VS2010+gtest使用总结

本文详细介绍了在Visual Studio 2010环境下如何配置和使用gtest测试框架,包括简易配置和专业配置两种方式,适用于不同规模的C++项目。通过设置项目属性、添加依赖库和头文件目录,以及编写测试用例,可以有效地进行单元测试。同时,文中还提到了gtest的一些使用技巧,如自定义输出信息、断言、全局事件以及运行参数的设定。注意事项中提醒了库版本匹配和运行库设置的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++项目开发离不开的测试框架—gtest! 刚刚啃掉这块骨头、总结一下以备后续使用。以下配置在VS2010+win7_64bit下:

下载配置流程

  1. 下载google test:https://github.com/google/googletest
  2. 解压,寻找目录msvc,直接运行sln文件,打开项目后直接编译全部,会在msvc目录下生成\gtest\Debug,找到gtestd.lib 和 gtest_maind.lib,接下来的使用参考如下两个方法:

简易配置–适用于小功能的验证

  1. 在vs2010中,新建一个C++控制台项目,例如 hello_gtest;
  2. 配置项目属性,在c/c++->常规->附加包含目录,输入 gtest下的include目录;在链接器->输入->附加依赖项,键入gtest下的\msvc\gtest\Debug\gtestd.lib以及gtest_maind.lib。
    在c/c++->代码生成->运行库,修改为“多线程调试(/mtd)” 。然后就可以正常编写代码了、

专业配置–适用于大项目的验证

为什么说这个方法适用于大项目呢?参考:http://www.cnblogs.com/SelaSelah/archive/2012/04/11/244252

[ 60%] Built target ydlidar_sdk [ 68%] Built target etlidar_test [ 76%] Built target tof_test [ 84%] Built target ydlidar_test [ 92%] Built target lidar_c_api_test [ 96%] Building CXX object test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o In file included from /usr/include/gtest/gtest-message.h:57, from /usr/include/gtest/gtest-assertion-result.h:46, from /usr/include/gtest/gtest.h:64, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.h:4, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.cpp:1: /usr/include/gtest/internal/gtest-port.h:279:2: error: #error C++ versions less than C++14 are not supported. 279 | #error C++ versions less than C++14 are not supported. | ^~~~~ /usr/include/gtest/gtest-assertion-result.h: In member function ‘void testing::AssertionResult::AppendMessage(const testing::Message&)’: /usr/include/gtest/gtest-assertion-result.h:207:48: error: ‘make_unique’ is not a member of ‘std’ 207 | if (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^~~~~~~~~~~ /usr/include/gtest/gtest-assertion-result.h:207:48: note: ‘std::make_unique’ is only available from C++14 onwards /usr/include/gtest/gtest-assertion-result.h:207:73: error: expected primary-expression before ‘>’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ /usr/include/gtest/gtest-assertion-result.h:207:75: error: expected primary-expression before ‘)’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ In file included from /usr/include/gtest/gtest-printers.h:122, from /usr/include/gtest/gtest-matchers.h:49, from /usr/include/gtest/internal/gtest-death-test-internal.h:47, from /usr/include/gtest/gtest-death-test.h:43, from /usr/include/gtest/gtest.h:65: /usr/include/gtest/internal/gtest-internal.h: At global scope: /usr/include/gtest/internal/gtest-internal.h:622:58: error: wrong number of template arguments (0, should be 1) 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^ In file included from /usr/include/c++/13/string:49, from /usr/include/c++/13/bits/locale_classes.h:40, from /usr/include/c++/13/bits/ios_base.h:41, from /usr/include/c++/13/iomanip:42, from /usr/include/gtest/gtest.h:54: /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/internal/gtest-internal.h:622:59: error: template argument 3 is invalid 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::AddTestName(const char*, int, const char*, const char*)’: /usr/include/gtest/internal/gtest-internal.h:599:23: error: request for member ‘insert’ in ‘((testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘int’} 599 | registered_tests_.insert( | ^~~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::TestExists(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:605:30: error: request for member ‘count’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 605 | return registered_tests_.count(test_name) > 0; | ^~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘const testing::internal::CodeLocation& testing::internal::TypedTestSuitePState::GetCodeLocation(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:609:40: error: qualified-id in declaration before ‘it’ 609 | RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name); | ^~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: ‘it’ was not declared in this scope; did you mean ‘int’? 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: request for member ‘end’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:611:12: error: ‘it’ was not declared in this scope; did you mean ‘int’? 611 | return it->second; | ^~ | int /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:725:75: error: wrong number of template arguments (0, should be 1) 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:725:76: error: template argument 3 is invalid 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::EqMatcher<Rhs>::EqMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:728:58: error: wrong number of template arguments (0, should be 1) 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:728:59: error: template argument 3 is invalid 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:728:61: error: expected ‘{’ before ‘(’ token 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:734:67: error: wrong number of template arguments (0, should be 1) 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:734:68: error: template argument 3 is invalid 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::NeMatcher<Rhs>::NeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:737:62: error: wrong number of template arguments (0, should be 1) 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:737:63: error: template argument 3 is invalid 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:737:65: error: expected ‘{’ before ‘(’ token 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:742:71: error: wrong number of template arguments (0, should be 1) 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:742:72: error: template argument 3 is invalid 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LtMatcher<Rhs>::LtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:745:54: error: wrong number of template arguments (0, should be 1) 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:745:55: error: template argument 3 is invalid 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:745:57: error: expected ‘{’ before ‘(’ token 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:750:74: error: wrong number of template arguments (0, should be 1) 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:750:75: error: template argument 3 is invalid 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GtMatcher<Rhs>::GtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:753:57: error: wrong number of template arguments (0, should be 1) 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:753:58: error: template argument 3 is invalid 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:753:60: error: expected ‘{’ before ‘(’ token 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:759:65: error: wrong number of template arguments (0, should be 1) 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:759:66: error: template argument 3 is invalid 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LeMatcher<Rhs>::LeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:762:60: error: wrong number of template arguments (0, should be 1) 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:762:61: error: template argument 3 is invalid 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:762:63: error: expected ‘{’ before ‘(’ token 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:768:68: error: wrong number of template arguments (0, should be 1) 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:768:69: error: template argument 3 is invalid 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GeMatcher<Rhs>::GeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:771:63: error: wrong number of template arguments (0, should be 1) 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:771:64: error: template argument 3 is invalid 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:771:66: error: expected ‘{’ before ‘(’ token 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest.h: At global scope: /usr/include/gtest/gtest.h:302:30: error: ‘std::enable_if_t’ has not been declared 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^~~~~~~~~~~ /usr/include/gtest/gtest.h:302:41: error: expected ‘>’ before ‘<’ token 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^ make[2]: *** [test/CMakeFiles/lidar_test.dir/build.make:76:test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:449:test/CMakeFiles/lidar_test.dir/all] 错误 2 make: *** [Makefile:166:all] 错误 2
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值