[英]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) 我是g测试的新手,这是我想要做的事情(在Linux服务器上从控制台):1)用C ++创建一个小项目(带有包含函数原型的头文件,带有函数的cpp文件)在它和另一个主调用已在头文件中定义的函数的cpp文件中)2)配置g测试以编写单元测试并测试在步骤1中创建的函数3)创建另一个带有几个单元测试的小项目(不同用于测试在步骤1)中在项目下创建的函数的方案
Can anyone please tell how to configure g test and the projects created with an example? 任何人都可以告诉我们如何配置g测试和用一个例子创建的项目?
Thanks in advance 提前致谢
First of all, get the most updated version of GoogleTest from the Subversion repository (you need Subversion installed): 首先,从Subversion存储库获取最新版本的GoogleTest(您需要安装Subversion ):
cd ~ svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only
Then, build the library (you need cmake installed): 然后,构建库(您需要安装cmake ):
mv googletest-read-only googletest mkdir googletest/lib cd googletest/lib cmake .. make
At this point: 在此刻:
To use googletest: 要使用googletest:
Include the header in your files: 在文件中包含标题:
#include "gtest/gtest.h"
Export the library path: 导出库路径:
export GOOGLETESTDIR=~/googletest
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 1)callMain.cpp,它调用该函数做一些操作
2) reverse.cpp which contains the logic of reversing a number and 2)reverse.cpp,其中包含反转数字的逻辑和
3) header.h containing the declaration of function prototypes 3)header.h包含函数原型的声明
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: 如果您通过gtest测试单元测试用例场景脚本(如unitTest1.cpp和unitTest2.cpp),则可以通过以下方式实现:
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. 这会编译并生成一个像test_try这样的可执行文件,在执行时会产生所需的结果。 Please correct me if I'm wrong anywhere. 如果我在任何地方都错了,请纠正我。 Happy coding :) 快乐编码:)
Please find the tutorial 请找到教程
@ http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html @ http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html
one correction at the makefile (test/src/Makefile). makefile上的一次更正(test / src / Makefile)。 The order of the library path is not correct!!. 库路径的顺序不正确!!
CXX = g++ CXX = g ++
CXXFLAGS = -g -L/opt/gtest/lib -lgtest -lgtest_main -lpthread CXXFLAGS = -g -L / opt / gtest / lib -lgtest -lgtest_main -lpthread
INCS = -I./ -I../../src -I/opt/gtest/include INCS = -I./ -I ../../ src -I / opt / gtest / include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o
testAll: $(OBJS) testAll:$(OBJS)
$(CXX) $(INCS) -o testAll Main_TestAll.cpp $(OBJS) $(CXXFLAGS) $(CXX)$(INCS)-o testAll Main_TestAll.cpp $(OBJS) $(CXXFLAGS)
.cpp.o: $(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS) .cpp.o:$(CXX)$(CXXFLAGS)-c $ <-o $ @ $(INCS)
clean: rm testAll *.o testAll.xml clean:rm testAll * .o testAll.xml
Today I read the Google Test FAQ . 今天我读了Google Test FAQ 。 It's not recommend to install a pre-compiled copy of Google Test(for example, into /usr/local
). 不建议安装Google Test的预编译副本(例如,安装到/usr/local
)。 You can find the answer in the FAQ. 您可以在常见问题解答中找到答案。
So, recommend this answer and this blog article . 所以,推荐这个答案和这篇博客文章 。
Following the CMake document of FindGTest . 遵循FindGTest的CMake文档 。
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: 然后,您可以使用命令:
cmake .
, generate Makefile ,生成Makefile make
, build gtest routine make
,构建gtest例程 ./fooTest
, run gtest routine ./fooTest
,运行gtest例程 make test
, run cmake test, it's another way you can run the gtest make test
,运行cmake test,这是你运行gtest的另一种方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.