Search code examples
c++testinggoogletestgooglemock

Can't build C/C++ program with gmock/gmock.h, too many errors are generated


I figure out the google test platform. I hope perhaps this Gtest platform is able to help my current project.

So, I didn't see any tutorial from Gtest team in order to guide that How to create a new project and compile the project while including both libs "gmock/gmork" and "gtest/gtest.h".

I downloaded the Gtest project from repo: google/googletest

Do some steps to be able to use Gtest framework:

  1. Intall gtest platform:

    $ sudo apt-get install libgtest-dev #gtest

    $ sudo apt-get install google-mock #gmock

  2. Then, install Cmake:

    $ sudo apt-get install cmake

  3. and build 2 projects (gtest and gmock)

    $ cd /usr/src/gtest

    $ sudo cmake CMakeLists.txt

    $ cd /usr/src/gmock

    $ sudo cmake CMakeLists.txt

    $ sudo make

  4. Finally, copy all *.a files to /usr/lib

    $ cp *.a /usr/lib

Consist of: libgtest.a , libgtest_main.a , libgmock.a , libgmock_main.a

I created an new project via Eclipse C, in Ubuntu 14.04 LTS. When I included gtest.h into my project, the program was built successfully and worked well

g++  -o "myGtest"  ./myGtest.o ./src_code.o   -lgtest -lpthread

but, if included more gmock.h, the program was failed to build. Compiler genereted too many errors

g++  -o "myGtest"  ./myGtest.o ./src_code.o   -lgtest -lgmock -lpthread

Hình ảnh nội tuyến

Please help me solve this problem.

By the way, I have one more question:

Assume that I have a simple module C src_code.c, for example:

Hình ảnh nội tuyến

I am testing function, in function invokes test , I would like to re-route the program to not execute this test, and it will jump to my self-defined test for instance, in myGtest.cpp, I write:

int test(int a) {
    printf("overridden successful !\n");
    return a;
}

This technique is a mock or stub or dummy function.

Please give me detail infor HOW can I mock a internal function in a module under test via Gtest framework ? Kindly give me an example.

I have tried to find solution at previous post at this site.

But, I didn't figure out my answer.


Solution

  • Gmock configuration [Eclipse Mar - Ubuntu 14.04 LTS]

    1. Clone master resource from google github

      git clone https://github.com/google/googletest.git GoogleTest/

    2. Install Cmake:

      sudo apt-get install cmake

    3. Build master Gtest project by Cmake:

      cd GoogleTest/googlemock

      sudo cmake CMakeLists.txt

      sudo make

    we will get log information:

       Scanning dependencies of target gmock
        [ 14%] Building CXX object CMakeFiles/gmock.dir/home/thaohm2/FPT/Softs/GoogleTest/googletest/src/gtest-all.cc.o
        [ 28%] Building CXX object CMakeFiles/gmock.dir/src/gmock-all.cc.o
        Linking CXX static library libgmock.a
        [ 28%] Built target gmock
        Scanning dependencies of target gmock_main
        [ 42%] Building CXX object CMakeFiles/gmock_main.dir/home/thaohm2/FPT/Softs/GoogleTest/googletest/src/gtest-all.cc.o
        [ 57%] Building CXX object CMakeFiles/gmock_main.dir/src/gmock-all.cc.o
        [ 71%] Building CXX object CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
        Linking CXX static library libgmock_main.a
        [ 71%] Built target gmock_main
        Scanning dependencies of target gtest
        [ 85%] Building CXX object gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
        Linking CXX static library libgtest.a
        [ 85%] Built target gtest
        Scanning dependencies of target gtest_main
        [100%] Building CXX object gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
        Linking CXX static library libgtest_main.a
        [100%] Built target gtest_main
    Four static libraries are generate:
    libgmock.a  libgmock_main.a  libgtest.a  libgtest_main.a
    
    1. We need to copy all *.a files to /usr/lib:

      sudo cp *.a /usr/lib

      sudo cp gtest/*.a /usr/lib

    And copy necessary libraries of gmock and gtest to /usr/include

    sudo cp -r include/gmock /usr/include/
    
    sudo cp -r ../googletest/include/gtest/ /usr/include/
    
    1. Create a new C++ project and add "gmock" and "pthread" options to Linker library Right click on Project --> Properties --> C/C++ Builder --> Setting --> Tool Settings --> GCC C++ Linker --> Libraries Add... gmock pthread

    Note that gmock must be frond of pthread in order.

    1. Include gmock and gtest to your project and carry out compiling the project:

      include

      include

      //...

      int main(int argc, char *argv[]) { ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); }

    Output:

    [==========] Running 0 tests from 0 test cases.
    [==========] 0 tests from 0 test cases ran. (0 ms total)
    [  PASSED  ] 0 tests.
    

    Done.