【发布时间】:2015-07-18 08:28:29
【问题描述】:
我承认我有一个独特的情况。我们使用 Make 构建我们的应用程序。但是我的 IDE CLion 使用 CMake。因此,我尝试将 GoogleTest 设置为在两者(某种)上运行。我可以两种方式编译我的代码(在命令行使用 make 并从我的 IDE 构建)。但是在 CLion 中,当我选择测试夹具并单击运行按钮时,没有找到任何测试,这就是我收到的:
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
这是我的测试夹具:
#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();
}
这是我的 CMakeLists.txt 文件:
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)
我在 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
【问题讨论】:
-
为什么要将测试放在匿名命名空间中?
-
请注意,我是 C++ 和 CMake 和 Make 以及 GoogleTest 的新手......(有很多要消化的东西,嗯?)。这就是他们在示例中的内容? !就此而言,我不确定是否应该将其放在名称空间中。我很想听听您的建议。
-
匿名命名空间很好,我有这样的测试,而且它也在示例中,应该不是问题。
标签: c++ linux makefile cmake googletest