简体   繁体   中英

How to configure and setup google test framework in linux

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

  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 

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 :)

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 $(CXXFLAGS) $(CXXFLAGS)

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

clean: rm testAll *.o testAll.xml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM