Mirek

About

Username
Mirek
Joined
Visits
16
Last Active
Roles
Members, Staff
Points
11
Posts
5
Badges
2
  • Re: Gmock integration

    I've just tried a simple example with recent C++test and latest gmock. It seems to be working fine. I'm talking about a simple use cases where gmock asserts/expectations messages go to console only (not visible in C++test reports), but full gmock power is available.

    For a simple scenario please follow the steps:

    • Build gmock and gtest
      Configuration with no pthreads mode is recommended
      (./configure --with-pthreads=no)

    • Add include paths to the compiler flags:
      Project Properties->Parasoft->C++test->Build Settings->Compiler options
      [append:] -Igmock_dir/include -Igtest_dir/include

    • Disable Parasoft Dynamic Stubs API
      (temporarily it conflicts with gtest, this will be addressed in near future)
      Project Properties->Parasoft->C++test->Build Settings->Compiler options
      [append:] -DCPPTEST_TRIGGER_ENABLED=0

    • Append gmock and gtest libs to the linker flags:
      Project Properties->Parasoft->C++test->Build Settings->Linker options
      [append:] gmock_dir/lib/.libs/libgmock.a gtest_dir/lib/.libs/libgtest.a

    • Add gmock framework initialization
      In your project create folder named factory or stubs (if not yet exists)
      Inside "factory" or “stubs” create a file init.cpp with the following content:
      include "gmock\gmock.h"
      void CppTest_Initialize(void)
      {
      static int argc = 1;
      static char * argv = {"fake cmd"};
      testing::InitGoogleMock( &argc, &argv );
      }
      This is to inject gmock initialization at the test harness startup.
      Currently there is no easy way to pass a real command line args (from main) to the InitGoogleMock.
      If needed, hardcode parameters via static argc and argv inside CppTest_Initialize.

    • Use Gmock inside the test suite:
      Snippet from my test suite:

      include "gmock/gmock.h"
      ...
      using ::testing::AtLeast;
      using ::testing::Return;
      ...
      class TestMock : public Test {
      public:
      MOCK_METHOD0( getValue, int() );
      };
      ...
      void TestSuite_my_class_cpp_4e0526f9::test_case_fuu()
      {
      TestMock test1;
      EXPECT_CALL(test1, getValue()).Times(AtLeast(2)).WillOnce(Return(33));
      int _ret = fuu(test1);
      CPPTEST_ASSERT_INTEGER_EQUAL(_ret, 33);
      }

    • Run prepared test cases
      Created mock objects will behave as expected (here return value from Test::getValue forced to 33)
      GMock expectations/asserts will be shown on the console:
      Expected to be called at least twice, but has only 1 WillOnce().C:\Users\mirek\parasoft\workspace_gmock\GMockTest\tests\autogenerated\TestSuite_my_class_cpp.cpp:58: Failure
      Actual function call count doesn't match EXPECT_CALL(test1, getValue())...
      Expected: to be called at least twice
      Actual: called once - unsatisfied and active

    All above was verified with Cygwin GNU GCC 5.3 (Windows 10)