How to set up and tune Google test environment on Linux

I am new to testing and here is what I am trying to do (on a Linux server from the console): 1) Create a small C ++ project (with a header file containing the function prototype, a cpp file with the function in it and another cpp file with the main function call already defined in the header file) 2) Set up a g-test to write unit tests and test the function created in step 1 3) Create another small project with several unit tests (different scripts to test the function created in the framework of the project in step 1 )

Can anyone tell me how to set up a g-test and projects created using an example?

Thank you in advance

+6
source share
4 answers
  • First of all, get the latest version of GoogleTest from the Subversion repository (you need Subversion ):

    cd ~ svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only 
  • Then create a library (you need cmake ):

     mv googletest-read-only googletest mkdir googletest/lib cd googletest/lib cmake .. make 
  • In this moment:

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

To use googletest:

  • Include the header in your files:

     #include "gtest/gtest.h" 
  • Export library path:

     export GOOGLETESTDIR=~/googletest 
  • Compile with

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

After a little research, here is what I found out:

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

And if you have scripts of a script like unit test, such as unitTest1.cpp and unitTest2.cpp, which will be checked through gtest, then this can be done 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 

Compiles and creates an executable file, for example test_try, which, when executed, gives the desired result. Please correct me if I am mistaken somewhere. Happy coding :)

0
source

Please find a study guide
@ http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html

Attention!!

one correction in the makefile (test / src / Makefile). The order of the library path is incorrect.

It would be like this:

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

0
source

New answer

Today I read the Google Test Frequently Asked Questions . It is not recommended to install a precompiled copy of the Google test (for example, in /usr/local ). You can find the answer in the FAQ.

So, I recommend this answer and this blog article .

Old answer

After the document CMake 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 use the command:

  • cmake . generate makefile
  • make , build a regular procedure
  • ./fooTest , start the normal procedure
  • make test , run cmake test, otherwise you can run gtest
0
source

Source: https://habr.com/ru/post/957495/


All Articles