googletest

玩转Google开源C++单元测试框架Google Test系列(gtest)(总)

醉酒当歌 提交于 2020-04-08 21:13:56
摘自: https://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Google Test,简称gtest,非常的不错。 我们原来使用的是自己实现的一套单元测试框架,在使用过程中,发现越来越多使用不便之处,而这样不便之处,gtest恰恰很好的解决了。 其实gtest本身的实现并不复杂,我们完全可以模仿gtest,不断的完善我们的测试框架, 但最后我们还是决定使用gtest取代掉原来的自己的测试框架,原因是: 1.不断完善我们的测试框架之后就会发觉相当于把gtest重新做了一遍,虽然轮子造的很爽,但是不是必要的。 2.使用gtest可以免去维护测试框架的麻烦,让我们有更多精力投入到案例设计上。 3.gtest提高了非常完善的功能,并且简单易用,极大的提高了编写测试案例的效率。 gtest的官方网站是: http://code.google.com/p/googletest/ 从官方的使用文档里,你几乎可以获得你想要的所有东西 http://code.google.com/p/googletest/wiki/GoogleTestPrimer http://code.google.com/p/googletest/wiki

How to get gtest TYPED_TEST parameter type

半城伤御伤魂 提交于 2020-02-02 03:27:39
问题 I have some unit tests written on Windows (Visual Studio 2017) and I need to port them on Linux (GCC 4.9.2 - I'm stuck with this version...). I've come with a simple example for my problem which compiles fine on Windows (I don't think it should compile as MyParamType is a dependent type from e template base class) and doesn't compile on Linux. Example: #include <gtest/gtest.h> template<typename T> struct MyTest : public testing::Test { using MyParamType = T; }; using MyTypes = testing::Types

Can I pass parameters to googletest test function

别说谁变了你拦得住时间么 提交于 2020-02-02 02:54:25
问题 After building my testfile, xxxxtest, with gtest can I pass a parameter when running the test, e.g. ./xxxxtest 100 . I want to control my test function using the parameter, but I do not know how to use the para in my test, can you show me a sample in test? 回答1: You could do something like the following: main.cc #include <string> #include "gtest/gtest.h" #include "my_test.h" int main(int argc, char **argv) { std::string command_line_arg(argc == 2 ? argv[1] : ""); testing::InitGoogleTest(&argc,

In GTest, how to check for number very close to 0? EXPECT_FLOAT_EQ not working

廉价感情. 提交于 2020-01-30 05:49:08
问题 I have some code that computes (a)sin/(a)cos values which are expected to be very close to 0, but when I say EXPECT_FLOAT_EQ(my_computed_var, 0); I get errors like: /path/to/my/test.cpp:148: Failure Expected: my_computed_var Which is: 9.9920072e-16 To be equal to: 0 How should I do this? I get this type of "error" for other numbers as well: -4.3711388e-08 Looking for both specific suggestions and general information about dealing with floating point error like this. 回答1: Use EXPECT_NEAR or

How to get backtrace information from exception in googletest?

核能气质少年 提交于 2020-01-29 21:53:23
问题 I'm trying to do some semi test driven design, and occasionally when I implement a new feature, it will have an exception somewhere. All gtest tells me is what the exception is, and does not give me any backtrace information. If I run gdb --args --gtest_catch_exceptions=0, it will stop at the test with the exception but not have any backtrace information. It simply states: [ RUN ] TESTNAME.test_case EXCEPTION: exception description[Inferior 1 (process 30528) exited with code 0377] (gdb) bt No

How to get backtrace information from exception in googletest?

吃可爱长大的小学妹 提交于 2020-01-29 21:48:53
问题 I'm trying to do some semi test driven design, and occasionally when I implement a new feature, it will have an exception somewhere. All gtest tells me is what the exception is, and does not give me any backtrace information. If I run gdb --args --gtest_catch_exceptions=0, it will stop at the test with the exception but not have any backtrace information. It simply states: [ RUN ] TESTNAME.test_case EXCEPTION: exception description[Inferior 1 (process 30528) exited with code 0377] (gdb) bt No

C++ unitTest with LinkTime substitution

别说谁变了你拦得住时间么 提交于 2020-01-25 07:22:17
问题 I'm reading "Test-Driven Development for Embedded C," by James W. Grenning. I'd like to reproduce case with "Link-Time Substitution" using Visual Studio Community 2019 and gTest. I have the following code: production_code staticly linked library foo.cpp #include "foo.h" int foo(int x) { return x + 1; } foo.h #ifndef _foo_ #define _foo_ int foo(int x); #endif //_foo_ In gtest project production_code library is included via reference test.cpp #include "gtest\gtest.h" #include "gmock\gmock.h"

Compiling Google test with Mingw-w64

妖精的绣舞 提交于 2020-01-25 04:16:25
问题 I'm trying to compile Google Test with Mingw-w64 in a Windows 10 machine but I always get the error: C:\git\tdd\googletest>cmake CMakeLists.txt -G "MinGW Makefiles" CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool. CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a

How do I use FRIEND_TEST with TYPED_TEST in Google Test?

雨燕双飞 提交于 2020-01-25 02:50:07
问题 In Google Test, I know how to use FRIEND_TEST. And I know how to use TYPED_TEST. But how do you make a TYPED_TEST also be a FRIEND_TEST? 回答1: Adding template <typename T> in front of the FRIEND_TEST declaration worked for me. 来源: https://stackoverflow.com/questions/29372942/how-do-i-use-friend-test-with-typed-test-in-google-test

find_package does not find GTest which is part of CMake

孤街浪徒 提交于 2020-01-23 02:38:07
问题 I want to find GTest via: find_package(GTest REQUIRED) But it is not found: Error:Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) I know from this link that GTest should be distributed via standard CMake. Can you tell me what I did wrong? 回答1: If you have gtest installed you can just do: add_subdirectory("/usr/src/gtest" ${CMAKE_BINARY_DIR}/gtest) enable_testing() include_directories(${GTEST_INCLUDE_DIRS}) add_executable(test test.cpp) target_link_libraries