How to configure and setup google test framework in linux

Sidaze picture Sidaze · Nov 6, 2013 · Viewed 12.2k times · Source

I'm a newbie to g test and Here is what I am trying to do (On a Linux server from console): 1) Create a small project in C++ ( with a header file containing a function prototype, a cpp file with a function in it and another cpp file with main calling the function already defined in the header file ) 2) Configure g test to write unit tests and test the function created in the step 1 3) Create another small project with a couple of unit tests (different scenarios to test the function created under the project in step 1)

Can anyone please tell how to configure g test and the projects created with an example?

Thanks in advance

Answer

Claudio picture Claudio · Nov 6, 2013
  1. First of all, get the most updated version of GoogleTest from the Subversion repository (you need Subversion installed):

    cd ~
    
    svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only
    
  2. Then, build the library (you need cmake installed):

    mv googletest-read-only googletest
    
    mkdir googletest/lib
    
    cd googletest/lib
    
    cmake ..
    
    make
    
  3. At this point:

    • compiled libraries are in the ~/googletest/lib directory
    • include files are in the ~/googletest/include directory

To use googletest:

  1. Include the header in your files:

    #include "gtest/gtest.h"
    
  2. Export the library path:

    export GOOGLETESTDIR=~/googletest
    
  3. Compile with

    g++ ... -I$GOOGLETESTDIR/include -L$GOOGLETESTDIR/lib -lgtest -lpthread