0
votes

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.

3

3 Answers

1
votes

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.

0
votes

Refer to this article on how to setup google test with eclipse:

http://www.codeproject.com/Articles/811934/Cplusplus-unit-test-start-guide-how-to-set-up-Goog

As for your second question: Google Mock is for mocking interfaces. You can set expectations and define actions of the interface functions. So your 'test' function has to be part of an interface in order to be able to mock it.

Please start with this tutorial:

https://github.com/google/googletest/blob/master/googlemock/docs/ForDummies.md

-1
votes

I think the main reason is mismatching between versions of Gtest and Gmock. I got them from one source of google:

https://github.com/google/googletest/tree/master/googletest

And carried out the above configuration. It worked fine for me.