Thursday, July 21, 2016

Compiling and running Google test and Google mock in Ubuntu

To compile and run Google Test and Google Mock in Ubuntu, you can follow these steps.

First download Google Test from here. Extract the downloaded zip file to a directory of your choice.

After extraction, you will see a folder named googletest-master. Enter this folder.

Inside the googletest-master folder, you will see these two folders: googletest and googlemock.

To compile and test run google test, go to the googletest directory. There you will see a make directory. Go inside the make directory and type 'make'.

Executing the make command will create the executable file, sample1_unittest. You can then run this executable file:

kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ pwd; ls
/home/kuyu/dkuyu/bin/googletest-master/googletest/make
Makefile
kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ make && ./sample1_unittest
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest-all.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating gtest_main.a
a - gtest-all.o
a - gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 6 tests.
kuyu@castor-ub:~/dkuyu/bin/googletest-master/googletest/make$ 

You can follow similar steps to compile and test run Google Mock in Ubuntu. From the googletest-master directory, go to the googlemock/make directory. Then type make. The executable created by the make command is named gmock_test.

You can actually study the Makefile file in the googletest/make and googlemock/make directories. There you can edit the GTEST_DIR, USER_DIR, and GMOCK_DIR (for googlemock).

1 comment:

  1. I have successfully installed googletest and googlemock using cmake as follows:

    $cd googletest-master
    #
    # now create a build directory and cd into it
    $mkdir mybuild
    $cd mybuild
    #
    # Build both the googletest and googlemock libraries
    # using Cmake. Note ../ will take you back to the CMakeLists.txt
    # in either the googletest or the googletest-master directory
    #
    $cmake -DBUILD_GTEST=ON -DBUILD_GMOCK=ON -DINSTALL_GTEST=ON -DINSTALL_GMOCK=ON ../
    # note $cmake -DBUILD_GMOCK=ON -DINSTALL_GMOCK=ON -DINSTALL_GTEST=ON ../
    # should work equally as well as BUILD_GMOCK=ON should build GTEST by default
    # build and install the libraries
    $sudo make install

    ReplyDelete