googletest

Create gmock tests for template specialization methods

断了今生、忘了曾经 提交于 2021-01-29 19:56:07
问题 I want to add GMOCK tests to verify if the container accesses the correct method. For vector it should access the second method, and for set it should access the first method (because set has set.find ). This is my template specialization: namespace tools{ struct low_priority {}; struct high_priority : low_priority {}; template<class TSource, class Ty> auto exists_in(high_priority, const TSource &source, const Ty &item) -> decltype(source->find(item) != source.end()) { return source.find(item

GTest CMake multiple definition of main

a 夏天 提交于 2021-01-29 09:40:35
问题 I am trying to learn how to use gtest for creating unit testing in c++. I wrote a simple library to test, where I created a factorial function src/main.cpp #include <iostream> int factorial(int n) { if (n == 1) return 1; return n * factorial(n - 1); } // Default main function int main() { std::cout << "Hello, World!" << std::endl; return 0; } src/main.h #ifndef GTEST_LEARN_MAIN_H #define GTEST_LEARN_MAIN_H int factorial(int n); #endif //GTEST_LEARN_MAIN_H src/CMakeLists.txt add_executable

How to solve GTest and LibTorch linkage conflict

爷,独闯天下 提交于 2021-01-29 09:32:55
问题 This question follows my precedent one. I'm writing a C++ program with OpenCV, Torch and NumCpp. The program compiles and works fine for now, but I need to write unit tests. I've followed google's tutorial to build GTest and GMock inside my project, but it fails. When I don't link Torch libraries, that works. Error when linking GTest + Torch : /usr/bin/ld: CMakeFiles/TryGTest_test.dir/test/boxTest.cpp.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>

Use c++ classes from console application class in other project visual studio

浪尽此生 提交于 2021-01-29 08:00:16
问题 I am trying to make a gtest project in Visual Studio for my console application in the same solution. When i create this project from the "new project" wizard i can choose the project i want to test. So i select my console application. After visual studio has created the gtest project i can see under references that my console project is present. When i try the sample test case everything works. But when i start to include my classes from the console application i get a linker error which

What is the inverse of EXPECT_DEATH?

对着背影说爱祢 提交于 2021-01-29 07:54:32
问题 With google tests, let's assume the following code #include <iostream> using namespace std; using MyFunc = void (*)(void); void foo_robust(MyFunc f) { if(f != nullptr) (*f)(); } void foo_not_robust(MyFunc f) { (*f)(); } void print(void) { cout << "hello world" << endl; } int main() { foo(&print); //works foo(nullptr); //runtime error ? return 0; } When using google test, I can do: TEST(TestAssertDeath, Death) { EXPECT_DEATH(foo(&print)); //will return FAILED, because does not die. EXPECT

How to mock a member variable of dependency?

て烟熏妆下的殇ゞ 提交于 2021-01-28 14:34:43
问题 I've got a class and member: class A { B obj; public: int f(int i){return obj.g(i);} } Here "B obj" is a dependency that requires run-time creation from a file. In my unit test for class A, I wish to mock "B obj", with a function g typed int(int). How can I write my test code, to mock "B obj", and then test A::f. Thanks a lot. 回答1: You need to use dependency injection to achieve this. To this end, have class B inherit from an interface, and have class A hold a pointer to that interface: class

How to mock a derived class that calls its base class methods?

故事扮演 提交于 2021-01-28 03:05:25
问题 I am unit testing a derived class and want to EXPECT_CALL that a certain method belonging to its base class is called. For example: class Base { public: void move(int x, int y); }; class Derived: public Base{ public: RESULT update(); private: int age; }; HRESULT Derived::update(void) { int param1 = 5, param2 = 10; move(param1, param2); age++; return SUCCESS; } I can't just create a mock for Derived and expect move since there is no dependency and the actual move() will be called. How can I be

Mocking a boost shared memory derived class with gtest

强颜欢笑 提交于 2021-01-28 02:54:34
问题 I have a simple CPP class storing some configuration of my project. This class is stored using boost interprocess shared memory, and so can be accessed from different processes running on my server. Now, I would like to run some tests on my program- so I wish to mock the functionality of my shared-memory-object. In order to do that with gtest, I've created a base configuration class, which my mock class and my shared memory class will derive from. In order to use gtest correctly, the base's

Google Test - generate values for template class instanciation

余生长醉 提交于 2021-01-28 00:14:09
问题 I am having an issue with Google Test. I have a code equivalent to what follows: A.h template<int N> class A { public: A() {} A(int n) { var = n; } int var; }; test_A.cpp #include "gtest/gtest.h" #include "A.h" template<typename T> class TestA : public ::testing::Test {}; TYPED_TEST_CASE_P(TestA); TYPED_TEST_P(TestA, SomeTest){ TypeParam x(0); EXPECT_EQ(x.var,0); ... } REGISTER_TYPED_TEST_CASE_P(TestA, SomeTest); typedef ::testing::Types<A<0>, A<1>, A<2>, A<3>, A<4>, A<5> ... A<511>, A<512>>

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

与世无争的帅哥 提交于 2021-01-21 10:22:37
问题 So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ? My production code : prod_code.cpp int TargetTestClass::targetFunc() { if(externalCFunc() == True) { statement1; statement2; statement3; /// and so on } } My testcode.cpp generally contains tests like this //Fixture for