10

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

Sidaze
  • 101
  • 1
  • 1
  • 4

4 Answers4

7
  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
    
Claudio
  • 10,614
  • 4
  • 31
  • 71
  • 1
    Look like the link to this post is not valid: svn: E170013: Unable to connect to a repository at URL 'http://googletest.googlecode.com/svn/trunk' – Kemin Zhou Dec 15 '16 at 20:55
0

After a small research here is what I found out:

If your project library contains files like:
1) callMain.cpp which calls the function to do some operations
2) reverse.cpp which contains the logic of reversing a number and
3) header.h containing the declaration of function prototypes

And if you have unit test case scenario scripts like unitTest1.cpp and unitTest2.cpp to be tested via gtest then, this can be achieved as follows:

g++ -I<gtest include directory location> -L<gtest directory location> <gtest_main.cc location> reverse.cpp unitTest1.cpp unitTest2.cpp -lgtest -lpthread -o test_try   

This compiles and produces an executable like test_try which when executed gives the desired result. Please correct me if I'm wrong anywhere. Happy coding :)

Martin G
  • 17,357
  • 9
  • 82
  • 98
Sidaze
  • 101
  • 1
  • 1
  • 4
0

Please find the tutorial
@ http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html

Caution!!

one correction at the makefile (test/src/Makefile). The order of the library path is not correct!!.

It would be like:

CXX = g++
CXXFLAGS = -g -L/opt/gtest/lib -lgtest -lgtest_main -lpthread
INCS = -I./ -I../../src -I/opt/gtest/include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o

testAll: $(OBJS)
$(CXX) $(INCS) -o testAll Main_TestAll.cpp $(OBJS) $(CXXFLAGS)

.cpp.o: $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)

clean: rm testAll *.o testAll.xml

Monir
  • 1,402
  • 14
  • 16
0

New answer

Today I read the Google Test FAQ. It's not recommend to install a pre-compiled copy of Google Test(for example, into /usr/local). You can find the answer in the FAQ.

So, recommend this answer and this blog article.

Old answer

Following the CMake document of FindGTest.

The code below works for me.

cmake_minimum_required(VERSION 2.8)

################################
# Add gtest environment
################################
enable_testing()
find_package(GTest REQUIRED)
# add gtest include directory: way 1
include_directories(${GTest_INCLUDE_DIRS})
# add gtest include directory: way 2
#include_directories(${GTest_SOURCE_DIRS}/include ${GTest_SOURCE_DIR})

################################
# Build tests
################################
aux_source_directory(. DIR_SRCS)
add_executable(fooTest ${DIR_SRCS})

# parameter `gtest` should at the front of `pthread`
target_link_libraries(fooTest gtest pthread)

# Take all gtest cases as one Cmake test case
add_test(AllFooTest fooTest)

And then, you can using command:

  1. cmake ., generate Makefile
  2. make, build gtest routine
  3. ./fooTest, run gtest routine
  4. make test, run cmake test, it's another way you can run the gtest
Community
  • 1
  • 1
James Shi
  • 1,894
  • 2
  • 13
  • 16