GoogleTest CMake和Make测试未运行 [英] GoogleTest CMake and Make tests not running
问题描述
我承认我有一个独特的情况。我们使用Make构建我们的应用程序。但是我的IDE,CLion,使用CMake。所以我试图设置GoogleTest运行在两种(类)。我可以编译我的代码两种方式(使用make在命令行和从我的IDE构建)。但是从CLion中选择测试夹具并点击运行按钮,没有发现测试,这是我收到的:
从gtest_main.cc运行main()
[==========]从0个测试用例运行0个测试。
[==========] 0个测试从0个测试用例运行。 (总共0毫秒)
[PASSED] 0测试。
过程完成退出代码0
这里是我的测试夹具: / p>
#include< gtest / gtest.h>
#includeOPProperties.h
namespace {
//用于测试类OPPropertiesTestTest的fixture。
class OPPropertiesTestTest:public :: testing :: Test {
protected:
//如果以下函数的主体
//为空,则可以删除任何或所有以下函数。
OPPropertiesTestTest(){
//您可以在这里为每个测试进行设置工作。
}
virtual〜OPPropertiesTestTest(){
//你可以做清理工作,不会在这里抛出异常。
}
//如果构造函数和析构函数不足以设置
//并清除每个测试,可以定义以下方法:
virtual void SetUp(){
//这里的代码将在构造函数之后立即被调用(每个测试前右
//)。
}
virtual void TearDown(){
//每次测试后立即调用代码(右
//在析构函数之前)。
}
//此处声明的对象可以由测试用例中的所有测试用于OPPropertiesTestTest。
};
TEST_F(OPPropertiesTestTest,ThisTestWillPass){
EXPECT_EQ(0,0);
}
TEST_F(OPPropertiesTestTest,ThisTestWillFail){
EXPECT_EQ(0,5);
}
} //命名空间
int main(int argc,char ** argv){
: :testing :: InitGoogleTest(& argc,argv);
return RUN_ALL_TESTS();
}
这是我的CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8)
pre>
项目(oneprint)
设置(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} -std = c + +11 -pthread -lgtest)
add_definitions(-DBOOST_LOG_DYN_LINK)
设置(SOURCE_FILES
src / controllers / OPProperties.cpp
src / controllers /OPProperties.h
src / main.cpp)
include_directories(src / controllers)
set(BOOST_ROOT/ usr / local / lib)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
#set(Boost_LIBRARY_DIR / usr / local / arm / lib )
set(OpenCV_LIBRARY_DIR / usr / include / opencv2)
set(Innovatrics_LIBRARY_DIR / usr / local / arm / lib)
find_package(OpenCV REQUIRED)
find_package(Boost 1.57.0 COMPONENTS文件系统线程日志计时系统原子程序要求)
include_directories($ {Boost_INCLUDE_DIRS})
enable_testing()
find_package(GTest REQUIRED)
include_directories($ {GTEST_INCLUDE_DIRS})
add_executable(OnePrint $ {SOURCE_FILES})
target_link_libraries(OnePrint $ {OpenCV_LIBS})
target_link_libraries {Boost_LIBRARIES})
target_link_libraries(OnePrint $ {Innovatrics_LIBRARY_DIR})
target_link_libraries(OnePrint $ {GTEST_BOTH_LIBRARIES})
target_link_libraries(OnePrint pthread)
我在src下添加了一个名为tests的文件夹,这是我的测试夹具OPPropertiesTestTest所在的位置。我还在顶层添加了一个测试文件夹。包含在此文件夹中的是Makefile和Srcs.mak文件。
以下是Makefile:
TARGET = oneprint
BASE = ../
-include $(BASE)Defs.x86.mak
-include $(BASE)OpenCV.mak
-include $(BASE)Boost。 mak
-include $(BASE)Innovatrics.mak
-include $(BASE)GTest.mak
-include $(BASE)Incl.mak
-include Srcs.mak
-include $(BASE)Common.mak
-include $(BASE)App.mak
这里是Srcs.mak文件:
VPATH = \
../src/controllers:\
../src:\
../src/tests
CPP_SRCS = \
OPProperties。 cpp \
#test files
CPP_SRCS + = \
OPPropertiesTest.cpp
解决方案我通常不会期望看到makefiles混合到一个CMake管理的项目;它听起来像你有相当复杂的设置?
除此之外,我认为这里的根本原因可能是双重的。我不认为在
/ src / tests /
中的测试可执行文件实际上正在构建。我不能看到你的CMakelists.txt中的任何东西,这将导致这是建立,所以除非你做一些额外的你没有显示我们,测试文件没有被编译。
这可能让它看起来像是,是你链接gtest提供的帮助库到你的
OnePrint
目标。这个辅助库lib是不寻常的,因为它实现一个main()
函数来保存用户指定自己的。
target_link_libraries(OnePrint $ {GTEST_BOTH_LIBRARIES})
从文档
GTEST_BOTH_LIBRARIES
是一个包含libgtest和libgtest-主要。你只需要libgtest,因为你已经写了自己的main()
。因此,您应改用GTEST_LIBRARIES
。
:CMake代码还有其他一些问题:
设置(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} -std = c ++ 11 -pthread -lgtest)
这不是跨平台无关你)例如MSVC将不会识别任何这些标志。如果没有问题,你需要将它包装在
if(UNIX)
或if(NOT MSVC)
块。
此外,这会设置全局编译器标志,即为项目中定义的每个目标。如果您需要更细致的控制,请查看
target_compile_options
的CMake v3.2文档,您可以指定这些每个目标。
另一个问题是
-lgtest
部分。这实际上是调用链接器链接gtest库。但是,CMake会为您处理这个问题 - 您在调用target_link_libraries(OnePrint $ {GTEST_LIBRARIES})
时执行此操作。
add_definitions(-DBOOST_LOG_DYN_LINK)
虽然这很好,但对全局应用的相同注释适用。每个目标的等效项为
target_compile_definitions
。
set(BOOST_ROOT/ usr / local / lib)
这可能是您的 机器上的Boost的位置,但其他开发者可能不是这样。这个变量真的是要由个人用户在调用CMake时指定(如果需要的话,很多路径会被CMake自动搜索) - 你不应该像这样把这样的路径硬编码到CMake文件中。
target_link_libraries(OnePrint pthread)
再次,
pthread
使用MSVC;这应该可能被包装在如果
块。
最后一点是,您可以在
target_link_libraries
命令中指定多个依赖关系,因此您可以将它们更改为:if(NOT MSVC)
set(PThreadLib pthread)
endif()
target_link_libraries(OnePrint
$ {OpenCV_LIBS}
$ {Boost_LIBRARIES}
$ {Innovatrics_LIBRARY_DIR}
$ {GTEST_LIBRARIES}
$ {PThreadLib})
I admit I have a unique situation. We build our application using Make. But my IDE, CLion, uses CMake. So I have tried to set up GoogleTest to run on both (kind of). I can compile my code both ways (using make at the command line and build from my IDE). But from within CLion when I select the test fixture and click the run button, no tests are found and this is what I receive:
Running main() from gtest_main.cc [==========] Running 0 tests from 0 test cases. [==========] 0 tests from 0 test cases ran. (0 ms total) [ PASSED ] 0 tests. Process finished with exit code 0
Here is my test fixture:
#include <gtest/gtest.h> #include "OPProperties.h" namespace { // The fixture for testing class OPPropertiesTestTest. class OPPropertiesTestTest : public ::testing::Test { protected: // You can remove any or all of the following functions if its body // is empty. OPPropertiesTestTest() { // You can do set-up work for each test here. } virtual ~OPPropertiesTestTest() { // You can do clean-up work that doesn't throw exceptions here. } // If the constructor and destructor are not enough for setting up // and cleaning up each test, you can define the following methods: virtual void SetUp() { // Code here will be called immediately after the constructor (right // before each test). } virtual void TearDown() { // Code here will be called immediately after each test (right // before the destructor). } // Objects declared here can be used by all tests in the test case for OPPropertiesTestTest. }; TEST_F(OPPropertiesTestTest, ThisTestWillPass) { EXPECT_EQ(0, 0); } TEST_F(OPPropertiesTestTest, ThisTestWillFail) { EXPECT_EQ(0, 5); } } // namespace int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8) project(oneprint) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lgtest") add_definitions(-DBOOST_LOG_DYN_LINK) set(SOURCE_FILES src/controllers/OPProperties.cpp src/controllers/OPProperties.h src/main.cpp) include_directories(src/controllers) set(BOOST_ROOT "/usr/local/lib") set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) unset(Boost_INCLUDE_DIR CACHE) unset(Boost_LIBRARY_DIRS CACHE) #set(Boost_LIBRARY_DIR /usr/local/arm/lib) set(OpenCV_LIBRARY_DIR /usr/include/opencv2) set(Innovatrics_LIBRARY_DIR /usr/local/arm/lib) find_package(OpenCV REQUIRED) find_package(Boost 1.57.0 COMPONENTS filesystem thread log chrono system atomic program_options REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) enable_testing() find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) add_executable(OnePrint ${SOURCE_FILES}) target_link_libraries(OnePrint ${OpenCV_LIBS}) target_link_libraries(OnePrint ${Boost_LIBRARIES}) target_link_libraries(OnePrint ${Innovatrics_LIBRARY_DIR}) target_link_libraries(OnePrint ${GTEST_BOTH_LIBRARIES}) target_link_libraries(OnePrint pthread)
I've added a folder under src called tests, which is where my test fixture, OPPropertiesTestTest, is located. I've also added a test folder at the top level. Included in this folder is a Makefile and a Srcs.mak file.
Here is the Makefile:
TARGET = oneprint BASE = ../ -include $(BASE)Defs.x86.mak -include $(BASE)OpenCV.mak -include $(BASE)Boost.mak -include $(BASE)Innovatrics.mak -include $(BASE)GTest.mak -include $(BASE)Incl.mak -include Srcs.mak -include $(BASE)Common.mak -include $(BASE)App.mak
Here is the Srcs.mak file:
VPATH = \ ../src/controllers:\ ../src:\ ../src/tests CPP_SRCS = \ OPProperties.cpp \ # test files CPP_SRCS += \ OPPropertiesTest.cpp
解决方案I wouldn't normally expect to see makefiles being mixed into a CMake-managed project; it sounds like you have quite a complicated setup?
That aside, I think the root cause here is likely to be twofold. I don't think the test executable in
/src/tests/
is actually being built. I can't see anything in your CMakelists.txt which would cause this to be built, so unless you're doing something extra you've not shown us, the test files aren't being compiled.What perhaps makes it look like they are, is that you're linking the helper lib which gtest provides into your
OnePrint
target. This helper lib is unusual in that it implements amain()
function to save users having to specify their own.You're doing this in the line
target_link_libraries(OnePrint ${GTEST_BOTH_LIBRARIES})
From the docs
GTEST_BOTH_LIBRARIES
is a variable containing both libgtest and libgtest-main. You only want libgtest since you've written your ownmain()
. So you should useGTEST_LIBRARIES
instead.
Note: There are a few other issues with the CMake code:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lgtest")
This isn't cross-platform (which may not matter to you) e.g. MSVC won't recognise any of these flags. If it does matter, you'd need to wrap this in an
if(UNIX)
orif(NOT MSVC)
block.Also, this sets the compiler flags globally, i.e. for every target defined in your project. If you need more fine-grained control, have a look at
target_compile_options
which lets you specify these per-target.Another issue is the
-lgtest
part. This is really a call to the linker to link the gtest library. However, CMake takes care of that for you - you're doing this when you calltarget_link_libraries(OnePrint ${GTEST_LIBRARIES})
.
add_definitions(-DBOOST_LOG_DYN_LINK)
While this is fine, the same comments about this being applied globally applies. The per-target equivalent is
target_compile_definitions
.
set(BOOST_ROOT "/usr/local/lib")
This is maybe the location of Boost on your machine, but that may well not be the case for other devs. That variable is really meant to be specified by individual users when they invoke CMake (if required... many paths are automatically searched by CMake) - you shouldn't really hard-code paths like this into the CMake file.
target_link_libraries(OnePrint pthread)
Again,
pthread
wouldn't be appropriate to link if you're using MSVC; this should probably be wrapped in anif
block.
The final trivial point is that you can specify more than one dependency in the
target_link_libraries
command, so you could change these to:if(NOT MSVC) set(PThreadLib pthread) endif() target_link_libraries(OnePrint ${OpenCV_LIBS} ${Boost_LIBRARIES} ${Innovatrics_LIBRARY_DIR} ${GTEST_LIBRARIES} ${PThreadLib})
这篇关于GoogleTest CMake和Make测试未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!