googletest

Mock non-virtual method giving compilation error

…衆ロ難τιáo~ 提交于 2019-12-01 09:41:09
I need to write the gtest to test some existing code that has a non-virtual method, hence I am testing using the below source, but I am getting the compilation error package/web/webscr/sample_template_class3.cpp: In function âint main()â: package/web/webscr/sample_template_class3.cpp:64: error: âclass Templatemyclassâ has no member named âgmock_displayâ sample_template_class3.cpp #include <iostream> #include <gtest/gtest.h> #include <gmock/gmock.h> using namespace std; template < class myclass> class Templatemyclass { private: myclass T; public : void display() { T.display(); } }; class Test {

gtest installed with conan: undefined reference to `testing::internal::GetBoolAssertionFailureMessage`

一笑奈何 提交于 2019-12-01 09:36:00
I use cmake to build my project and conan to install Google Test as dependency: conanfile.txt [requires] gtest/1.7.0@lasote/stable [generators] cmake [imports] bin, *.dll -> ./build/bin lib, *.dylib* -> ./build/bin CMakeLists.txt PROJECT(MyTestingExample) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) INCLUDE(conanbuildinfo.cmake) CONAN_BASIC_SETUP() ADD_EXECUTABLE(my_test test/my_test.cpp) TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS}) test/my_test.cpp #include <gtest/gtest.h> #include <string> TEST(MyTest, foobar) { std::string foo("foobar"); std::string bar("foobar"); ASSERT_STREQ(foo.c_str(), bar.c_str

gtest installed with conan: undefined reference to `testing::internal::GetBoolAssertionFailureMessage`

大兔子大兔子 提交于 2019-12-01 08:40:47
问题 I use cmake to build my project and conan to install Google Test as dependency: conanfile.txt [requires] gtest/1.7.0@lasote/stable [generators] cmake [imports] bin, *.dll -> ./build/bin lib, *.dylib* -> ./build/bin CMakeLists.txt PROJECT(MyTestingExample) CMAKE_MINIMUM_REQUIRED(VERSION 2.8) INCLUDE(conanbuildinfo.cmake) CONAN_BASIC_SETUP() ADD_EXECUTABLE(my_test test/my_test.cpp) TARGET_LINK_LIBRARIES(my_test ${CONAN_LIBS}) test/my_test.cpp #include <gtest/gtest.h> #include <string> TEST

Google Test separate project - How to get tests running against the C++ project

本小妞迷上赌 提交于 2019-12-01 08:25:57
问题 I am trying to figure out how to run Google Test against my C++ project using CMake. So far I have created a project called Simple and a Google Test project called SimpleTest. For the Simple Project Here's my CMakeLists.txt file: cmake_minimum_required(VERSION 2.8.4) project(Simple) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp NewCppClass.cpp NewCppClass.h) add_executable(Simple ${SOURCE_FILES}) Here's my main.cpp file: #include <iostream> #include

GTest's output has no colors when built with cmake+ninja and executed automatically

有些话、适合烂在心里 提交于 2019-12-01 04:39:07
I'm trying to configure CMake and ninja as a build system for my project. Except the app itself I have an extra executable for unit tests powered by gtest. I thought it would be nice to have them executed automatically whenever they are built. Here's how I made it: ├── build └── source ├── CMakeLists.txt ├── main.cc └── ut ├── CMakeLists.txt ├── gtest │ ├── ... └── ut.cc source/CMakeLists.txt... cmake_minimum_required (VERSION 2.6) project (trial) add_subdirectory(ut) add_executable(trial main.cc) ...and source/ut/CMakeLists.txt: add_subdirectory(gtest) include_directories ("gtest/include")

Verifying exception messages with GoogleTest

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 03:39:58
Is it possible to verify the message thrown by an exception? Currently one can do: ASSERT_THROW(statement, exception_type) which is all fine and good but no where can I find a way to test e.what() is really what I am looking for. Is this not possible via google test? Something like the following will work. Just catch the exception somehow and then do EXPECT_STREQ on the what() call: #include "gtest/gtest.h" #include <exception> class myexception: public std::exception { virtual const char* what() const throw() { return "My exception happened"; } } myex; TEST(except, what) { try { throw myex; }

What's the way to access argc and argv inside of a test case in Google Test framework?

萝らか妹 提交于 2019-12-01 03:38:24
I’m using Google Test to test my C++ project. Some cases, however, require access to argc and argv to load the required data. In the main() method, when initializing, argc and argv are passed to the constructor of testing. testing::InitGoogleTest(&argc, argv); How can I access them later in a test? TEST(SomeClass, myTest) { // Here I would need to have access to argc and argv } sbi I don't know google's test framework, so there might be a better way to do this, but this should do: //--------------------------------------------- // some_header.h extern int my_argc; extern char** my_argv; // eof

Google mock ByRef method

对着背影说爱祢 提交于 2019-12-01 02:59:27
I have a class that takes a boolean as a reference parameter and returns an integer: class Foo { public: Bar my_bar; virtual int myMethod(bool &my_boolean) = 0; } /*...*/ int Foo::myMethod(bool &my_boolean){ if (my_bar == NULL){ my_boolean = false; return -1; } else{ my_boolean = true; return 0; } } And I created a mock for this class: class MockFoo : public Foo { MOCK_METHOD1(myMethod,int(bool &my_boolean)); } I'm having problems on how to set the expectations for this kind of function,because I need to set the return value and the reference parameter to specific values to properly create my

SEH exception with code 0xc0000005 thrown in the test body

纵饮孤独 提交于 2019-12-01 02:42:52
I am writing a test using GoogleTest for the following class and I am getting the above error. class Base { // Other Functions; CSig objSig[50]; } The Class CSig is as follows: class CSig { //... constructor, destructor(empty) and some functions CMod *objMod; CDemod *objDemod; } CSig :: CSig { bIsInitialised = false; for (int i=0; i<MAX_NUM; i++) { PStrokePrev[i] = 0.0; } } However, when I discard CSig objSig[50] , the tests run fine. What can I do to solve this issue? Also, I need to have CSig objSig[50] in the Base class. A SEH (Structured Exception Handling) exception is not a C++-exception

Google mock ByRef method

狂风中的少年 提交于 2019-11-30 23:43:45
问题 I have a class that takes a boolean as a reference parameter and returns an integer: class Foo { public: Bar my_bar; virtual int myMethod(bool &my_boolean) = 0; } /*...*/ int Foo::myMethod(bool &my_boolean){ if (my_bar == NULL){ my_boolean = false; return -1; } else{ my_boolean = true; return 0; } } And I created a mock for this class: class MockFoo : public Foo { MOCK_METHOD1(myMethod,int(bool &my_boolean)); } I'm having problems on how to set the expectations for this kind of function