Search code examples
c++tddgooglemockcppunit

TestPlugInRunnerd.exe + gmock


We build our cppunit unittests as a dll and load it into TestPlugInRunnerd.exe to show our results. We write our own mocks but I'd like to start using a mocking framework such as gmock.

I downloaded gmock and linked against it without much in the way of problems. I have written a mock using gmock and it compiles fine. But then I read the following in the gmock faq:

If you want to use something other than Google Test (e.g. CppUnit or CxxTest) as your testing framework, just change the main() function in the previous section to:

int main(int argc, char** argv) {
  // The following line causes Google Mock to throw an exception on failure,
  // which will be interpreted by your testing framework as a test failure.
  ::testing::GTEST_FLAG(throw_on_failure) = true;
  ::testing::InitGoogleMock(&argc, argv);
  ... whatever your testing framework requires ...
}

This approach has a catch: it makes Google Mock throw an exception from a mock object's destructor sometimes. With some compilers, this sometimes causes the test program to crash. You'll still be able to notice that the test has failed, but it's not a graceful failure. 

I obviously don't have a main. What do I need to do to get gmock to work with my dll? Should I consider alternatives to gmock?

Thanks,

Barry


Solution

  • First, it is not possible that you don't have a main. Otherwise how would you execute your unit tests?

    Second, you could create a static variable of a class where you call these two functions, like this :

    struct GmockInitializer
    {
      GmockInitializer()
      {
        ::testing::GTEST_FLAG(throw_on_failure) = true;
        ::testing::InitGoogleMock(0,0);  // << not sure about this. might not work
      }
    };
    GmockInitializer gmockInitializer;