我正在尝试将GTest框架集成到我的项目中,但我不确定如何。我正在使用CMake来构建它。理想情况下,我想为运行测试创建一个单独的可执行文件,我希望能够选择在单个模块或整个项目上运行测试。我也希望它与CTest合作,但这不是优先事项。我该如何设置?我的项目结构如下:
cmake_minimum_required(VERSION 3.1)
project(Aura)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
include_directories(include)
add_subdirectory(src)
set (run_src
src/run.cpp
)
add_executable(aura ${run_src})
target_link_libraries(aura util)
这是我的主要CMakeLists.txt:
{{1}}
答案 0 :(得分:1)
我可以帮助理解本身,你应该知道如何使它适应你的项目结构。所以一般来说,你想要使你的程序结构如下:
Structure:
|────CMakeLists.txt
├─── client
───────CMakeLists.txt
├─── libs
───────CMakeLists.txt
├─── tests
───────CMakeLists.txt
───────googletest
───────googletest
───────googlemock
top cmake可以简单包含子目录:
cmake_minimum_required(VERSION 3.1)
project(Aura)
set (CMAKE_CXX_STANDARD 14)
add_subdirectory(client)
add_subdirectory(test)
add_subdirectory(lib)
将使您的应用程序可执行的客户端cmake将如下所示:
project(AuraClient)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} )
set(Aura-SRCS
someSource.cpp
main.cpp)
set(Aura-HEADERS
someSource.h)
add_executable(Aura ${Aura-SRCS} ${Aura-HEADERS})
target_link_libraries(Aura auraLib)
lib,您将保留逻辑代码:
project(AuraLib)
set(lib_SRCS
SomeLogic.cpp
)
set(lib_HEADERS
SomeLogic.h
)
add_library(auraLib ${lib_SRCS} ${lib_HEADERS})
和你的测试cmake:
enable_testing()
project(AuraTest)
set(googleTest_Dir ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
if (EXISTS ${googleTest_Dir})
SET(GTestSrc ${googleTest_Dir}/googletest)
SET(GMockSrc ${googleTest_Dir}/googlemock)
else ()
message(FATAL_ERROR "No googletest src dir found!")
endif ()
include_directories(${GTestSrc} ${GTestSrc}/include ${GMockSrc} ${GMockSrc}/include)
include_directories(${CMAKE_HOME_DIRECTORY} ${CMAKE_HOME_DIRECTORY}/test)
set(test_HEADERS
StdInc.h
)
set(test_SOURCES
main.cpp
PointTest.cpp)
add_executable(AuraTest ${test_SOURCES} ${test_HEADERS} ${mock_HEADERS} ${GTestSrc}/src/gtest-all.cc ${GMockSrc}/src/gmock-all.cc)
target_link_libraries(AuraTest auraLib)
答案 1 :(得分:0)
你应该像添加目标aura
一样添加测试可执行文件,并包含gtest includes和link gtest库。
将以下行添加到您的CMakeLists.txt:
include_directories(3rdparty/gtest/include)
link_directories(3rdparty/gtest/lib)
add_executable(moduleTest1 moduleTest1.cpp)
target_link_libraries(moduleTest1 gtest)
如果有子模块的其他链接,您也应该链接它们。例如,如果需要module1,也可以与module1链接,例如target_link_libraries(moduleTest1 gtest module1)