我有一个项目,结构如下:
|--assets/
|--core/
|--deps/
|--Catch2/
|--win32/
|--# Have Catch2 library installed here
|--include/
|--# Nothing here
|--src/
|--sample.cpp # No content in this file
|--tests/
|--test.cpp
|--CMakeLists.txt
|--main.cpp
|--CMakeLists.txt
CMakeLists.txt的顶级内容是:
cmake_minimum_required (VERSION 3.8)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
project("My.Project")
add_executable(MyProject main.cpp)
# Copy all DLLs if windows:
if(WIN32)
file(GLOB_RECURSE DYNAMIC_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/*.dll")
foreach(dll ${DYNAMIC_LIBS})
add_custom_command(TARGET AZTEC_EDITOR POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${dll} $<TARGET_FILE_DIR:AZTEC_EDITOR>)
endforeach()
else(APPLE)
endif()
add_subdirectory(core)
target_link_libraries(MyProject MyLib)
“核心”文件夹中的CMakeLists.txt文件为:
file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp")
add_library(MyLib ${HEADER_FILES} ${SOURCE_FILES} ${TEST_FILES})
target_include_directories(MyLib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Copy all DLLs if windows:
if(WIN32)
file(GLOB_RECURSE DYNAMIC_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/*.dll")
foreach(dll ${DYNAMIC_LIBS})
add_custom_command(TARGET AZTEC_EDITOR_CORE POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${dll} $<TARGET_FILE_DIR:AZTEC_EDITOR_CORE>)
endforeach()
else(APPLE)
endif()
# Catch2 stuff:
if(WIN32)
find_package(Catch2 REQUIRED PATHS "${CMAKE_CURRENT_SOURCE_DIR}/deps/catch2/win32")
target_link_libraries(MyLib Catch2::Catch2)
endif()
include(CTest)
include(Catch)
catch_discover_tests(MyLib)
我的test.cpp
内容(来自Catch2文档,该测试应失败):
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
unsigned int Factorial(unsigned int number)
{
return number <= 1 ? number : Factorial(number - 1)*number;
}
TEST_CASE("Factorials are computed", "[factorial]")
{
REQUIRE(Factorial(1) == 2); // Should fail here.
REQUIRE(Factorial(2) == 2);
REQUIRE(Factorial(3) == 6);
REQUIRE(Factorial(10) == 3628800);
}
通常,当我使用cmake -G "Visual Studio 15" . -B .\build
生成Visual Studio文件时,发现测试时,我会看到一个名为“ RUN_TESTS”的项目,该项目分组在“ CMakePredefinedTargets”下。但是,我看不到这个项目了。
此外,当我构建项目(使用Visual Studio 2017)时,测试未运行。请帮忙。谢谢。
答案 0 :(得分:1)
根据@Stephen Newell和@ squareskittles的评论,我最终修改后的工作CMakeLists.txt文件如下。
我将enable_testing()
添加到了根文件。这使RUN_TESTS项目在Visual Studio中可见。我想运行测试时构建这个项目:
cmake_minimum_required (VERSION 3.8)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
project("My.Project")
enable_testing()
add_executable(MyProject main.cpp)
# Copy all DLLs if windows:
if(WIN32)
file(GLOB_RECURSE DYNAMIC_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/*.dll")
foreach(dll ${DYNAMIC_LIBS})
add_custom_command(TARGET AZTEC_EDITOR POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${dll} $<TARGET_FILE_DIR:AZTEC_EDITOR>)
endforeach()
else(APPLE)
endif()
add_subdirectory(core)
target_link_libraries(MyProject MyLib)
但是,仅添加这一行仍然存在一个问题,即使RUN_TESTS在那里,它也没有发现单元测试,因为CMake-Catch2集成需要一个可执行文件而不是一个库来运行测试。因此,我在核心文件夹的CMakeLists.txt文件中添加了一个单独的目标:
file(GLOB HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h")
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_library(MyLib ${HEADER_FILES} ${SOURCE_FILES})
target_include_directories(MyLib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Copy all DLLs if windows:
if(WIN32)
file(GLOB_RECURSE DYNAMIC_LIBS "${CMAKE_CURRENT_SOURCE_DIR}/*.dll")
foreach(dll ${DYNAMIC_LIBS})
add_custom_command(TARGET AZTEC_EDITOR_CORE POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${dll} $<TARGET_FILE_DIR:AZTEC_EDITOR_CORE>)
endforeach()
else(APPLE)
endif()
# Catch2 stuff:
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp")
add_executable(MyLibTests ${TEST_FILES})
if(WIN32)
find_package(Catch2 REQUIRED PATHS "${CMAKE_CURRENT_SOURCE_DIR}/deps/catch2/win32")
target_link_libraries(MyLibTests Catch2::Catch2)
endif()
include(CTest)
include(Catch)
catch_discover_tests(MyLibTests)
由于Catch2文档中给出的示例的希望很小,所以这将有助于尝试将Catch2与更复杂的项目结构集成的人。感谢评论员的所有帮助。