googletest

Google Mock: Is it ok to use global mock objects?

早过忘川 提交于 2019-12-05 18:17:07
In all the documentation about gmock I always find the mock object to be instantiated inside a test, like that: TEST(Bim, Bam) { MyMockClass myMockObj; EXPECT_CALL(MyMockObj, foo(_)); ... } So, the object is created and destroyed per test. I believe it's also perfectly fine to create and destroy the object per test fixture . But I'm wondering if it's also ok to have a file-global instance of the mock object, like that: MyMockClass myMockObj; TEST(Bim, Bam) { EXPECT_CALL(MyMockObj, foo(_)) ... } I tried it and I have absolutely no problems so far, it all seems to work fine. But maybe I should

Why does Google Test/Mock show leaked mock object error by std::unique_ptr?

蓝咒 提交于 2019-12-05 18:16:44
Let's assume that there is Bar object which uses a Foo object. The ownership is exclusive, so Bar gets Foo as a std::unique_ptr in its constructor. I would like to test Bar with Google Test framework, so I made a following code: using namespace testing; class Foo { public: virtual int F() = 0; }; class Bar { public: Bar(std::unique_ptr<Foo>&& foo) : m_foo(std::move(foo)) { } int B() { return m_foo->F(); } private: std::unique_ptr<Foo> m_foo; }; class MockFoo : public Foo { public: MOCK_METHOD0(F, int()); }; class BarTest : public Test { }; TEST_F(BarTest, Test1) { auto mock_foo = std::make

Google Mock: why NiceMock does not ignore unexpected calls?

北城以北 提交于 2019-12-05 12:27:33
I am using Google Mock 1.7.0 with Google Test 1.7.0. The problem is when I use NiceMock I get test failures because of unexpected mock function call (which should be ignored by NiceMock as per Google Mock documentation). The code looks like this: // Google Mock test #include <gtest/gtest.h> #include <gmock/gmock.h> using ::testing::Return; using ::testing::_; class TestMock { public: TestMock() { ON_CALL(*this, command(_)).WillByDefault(Return("-ERR Not Understood\r\n")); ON_CALL(*this, command("QUIT")).WillByDefault(Return("+OK Bye\r\n")); } MOCK_METHOD1(command, std::string(const std::string

GoogleMock and GoogleTest in Visual Studio 2010

廉价感情. 提交于 2019-12-05 09:13:57
问题 Has anyone successfully built gmock and gtest in Visual Studio 2010? I've tried with version 1.5.0, but I only get incomprehensible compilation errors. 回答1: I found this thread in google groups about issues found when building gmock-1.5.0 under VS2010. Following the thread, I've created a short readme file, which worked for me, so here it is: Download gmock 1.5.0 from Google Mock. Extract to library folder on the machine (e.g. C:\Libs\gmock-1.5.0). From now on, this folder will be reffered as

How to catch an assert with Google test?

北城余情 提交于 2019-12-05 08:27:29
问题 I'm programming some unit test with the Google test framework. But I want to check whether some asserts are well placed and are useful. Is there a way to catch an assert in Google test? Example code under test: int factorial(int n){ assert(n >= 0); //.... } And then the test: #include <gtest/gtest.h> TEST(FactorialTest,assertNegative){ EXPECT_ANY_THROW({ factorial(-1); }); } But EXPECT_ANY_THROW doesn't catch the assert but only exceptions. I'm searching for a solution to catch asserts. 回答1:

How to use gmock to test that a class calls it's base class' methods

筅森魡賤 提交于 2019-12-05 08:22:14
class Foo { public: int x; int y; void move(void); }; class SuperFoo: public Foo { public: int age; void update(); }; SuperFoo::update(void) { move(); age++; } I'm just starting out with C++ and unit testing, I have some code resembling the above and I want to use gmock to test that SuperFoo::update() calls the base class' move() method. What would be that best way to attack this type of situation? One way is to make the move method virtual, and create a mock of your class: #include "gtest/gtest.h" #include "gmock/gmock.h" class Foo { public: int x; int y; virtual void move(void); //^^^^

Can I pass parameters to googletest test function

若如初见. 提交于 2019-12-05 08:14:43
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? 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, argv); testing::AddGlobalTestEnvironment(new MyTestEnvironment(command_line_arg)); return RUN_ALL_TESTS();

How to install Google Test on Ubuntu without root access?

纵饮孤独 提交于 2019-12-05 05:35:52
问题 I am trying to install Google Test according to this answer on Ubuntu without root access, as I need learn and use it at work. Managed to get these done in my own user folder: $ mkdir ~/temp $ cd ~/temp $ unzip gtest-1.7.0.zip $ cd gtest-1.7.0 $ mkdir mybuild $ cd mybuild $ cmake -DBUILD_SHARED_LIBS=ON -Dgtest_build_samples=ON -G"Unix Makefiles" .. $ make It seems I already have gtest in /usr/src/gtest altough I don't want to use this, because it was not me who installed it and I'm not sure

How to hide targets in Visual Studio from CMake

南楼画角 提交于 2019-12-05 04:52:11
I am generating a .sln with CMake. I want to use Google Test and use that kind of code for adding a new tests: add_executable(my_test test/my_test.cpp) target_link_libraries(my_test gtest gmock_main) add_test(NAME my_test COMMAND my_test) It works fine, but when I open my .sln, I have all the targets appearing in the solution explorer: the libraries, the unit tests, etc. Is there a way to hide these target? You can't do it explicitly only in cmake (ATM), but here is one way on how can hide multiple targets more efficiently: Simply put them on in the same "folder" (in cmake) and then hide the

Get google test exception throw message [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-05 03:35:00
This question already has an answer here: Verifying exception messages with GoogleTest 1 answer I am using google Test framework for my project. I am throwing exception from the code as: throw DerivedClassException("message"); and in the test frame using as: ASSERT_THROW(commond(), DerivedClassException); I want to get message with what() API. Any way to get exact exception message of the exception. The only way to check the thrown exception is to catch it in the test : void test_foo( MyTest, TestException ) { try { functionThatThrowsException(); FAIL(); } catch( const DerivedClassException&