googletest

How to use GTest with CMake ? Linkage problem when following Google's guide

假装没事ソ 提交于 2020-07-07 11:40:46
问题 I started to use boost test for my project, but I need to mock static methods, so I try to switch to GTest and GMock. I followed the really clear guide from google, and the CMakeLists seems to be doing its job : CMakeLists.txt cmake_minimum_required(VERSION 3.15) project(POC_V4) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) # Specifying we are using pthread for UNIX systems. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -pthread -Wall") find_package(OpenCV

gmock/google-mock issues warning and fails the test with mocking exceptions

强颜欢笑 提交于 2020-06-17 09:45:52
问题 I have coded a demo mock using google mock. The issue is that it is failing and not properly mocking. I cannot understand the issue here. Code: test/mock_turtle_test.cc #include "mock_turtle.h" #include "../painter.h" #include <gtest/gtest.h> using ::testing::_; using ::testing::AtLeast; using ::testing::Return; ACTION(MyException) { throw(error_k()); } TEST(PainterTest, CanDrawSomething) { Painter painter; EXPECT_CALL(*(painter.getTurtle()), PenDown()) .Times(AtLeast(1)) .WillOnce

How to set up both GoogleTest and GoogleMock in Visual Studio?

╄→гoц情女王★ 提交于 2020-06-01 05:07:32
问题 With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest versions anyway). But what about using GoogleMock as well? You would think that since Google combined gtest/gmock some time ago that this would just work. Just #include "gmock/gmock.h" and mock away. But no, the GoogleTest NuGet package that is automatically added by the template does not include the gmock folder at all.

How to set up both GoogleTest and GoogleMock in Visual Studio?

荒凉一梦 提交于 2020-06-01 05:06:48
问题 With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest versions anyway). But what about using GoogleMock as well? You would think that since Google combined gtest/gmock some time ago that this would just work. Just #include "gmock/gmock.h" and mock away. But no, the GoogleTest NuGet package that is automatically added by the template does not include the gmock folder at all.

google mock - can I call EXPECT_CALL multiple times on same mock object?

对着背影说爱祢 提交于 2020-05-20 09:33:41
问题 If I call EXPECT_CALL twice on the same mock object in the same TEST_F . . . what happens? Are the expectations appended to the mock object or does the second call erase the effects of the first call? I found The After Clause which appears to imply that multiple calls to same mock + EXPECT_CALL are allowed. 回答1: Yes, you can call EXPECT_CALL on the same mock object multiple times. As long as you assure that all EXPECT_CALL were called before the mocked methods were actually used. Otherwise

How to use gmock to mock up a std::function?

北慕城南 提交于 2020-05-16 03:25:07
问题 The constructor of my class is A( ... std::function<bool(const std::string&, const std::string&)> aCallBack, ... ); I want to use EXPECT_CALL to test it. This callback is from another class B. I created a Mock like class BMock : public B { MOCK_METHOD2( aCallBack, bool(const std::string&, const std::string&) ); } Then I tried B *b = new B(); std::function<bool(const std::string&, const std::string&)> func = std::bind(&B::aCallBack, b, std::PlaceHolders::_1, std::PlaceHolders::_2); It still

Check if the function has been called in gtest

邮差的信 提交于 2020-05-15 05:08:46
问题 In gtest framework, is there any way to check whether a function has been called? (without gmock, use gtest only) for example: class a { public: void dd() {...}; void cc() {...}; void bb() {...}; void aa() { bb(cc(dd())); } }; void main () { a dut; dut.aa(); } I do not care the function input and even the correctness of the output. I just want to know if the function (e.g. aa()) has been triggered. Is there any solution? Many thanks in advance! 回答1: without gmock, use gtest only That's a very

Check if the function has been called in gtest

天大地大妈咪最大 提交于 2020-05-15 05:08:12
问题 In gtest framework, is there any way to check whether a function has been called? (without gmock, use gtest only) for example: class a { public: void dd() {...}; void cc() {...}; void bb() {...}; void aa() { bb(cc(dd())); } }; void main () { a dut; dut.aa(); } I do not care the function input and even the correctness of the output. I just want to know if the function (e.g. aa()) has been triggered. Is there any solution? Many thanks in advance! 回答1: without gmock, use gtest only That's a very

Interleaving EXPECT_CALL()s and calls to the mock functions

不想你离开。 提交于 2020-05-15 04:06:59
问题 The Google Mock documentation says: Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined . In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions. Does anyone know any details behind this restriction? I have a number of unit tests which definitely violate this rule but seem to function properly, however. 回答1: I have to disagree with @Marko Popovic's assessment, and believe what he's

Interleaving EXPECT_CALL()s and calls to the mock functions

前提是你 提交于 2020-05-15 04:03:35
问题 The Google Mock documentation says: Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined . In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions. Does anyone know any details behind this restriction? I have a number of unit tests which definitely violate this rule but seem to function properly, however. 回答1: I have to disagree with @Marko Popovic's assessment, and believe what he's