CMake:如何设置库的单元测试
CMake: How to setup unit testing of a library
我在一个kata项目下工作,学习如何用C++编写单元测试(链接到存储库(。该项目中的一个元素是DictionaryPath库。它被放在一个单独的目录中,有专用的CMakeFile.txt
:
cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
add_library(DictionaryPath
include/DictionaryPath/Dictionary.h
src/Dictionary.cpp
include/DictionaryPath/DictionaryPath.h
src/DictionaryPath.cpp
src/WordsGraph.cpp
src/WordsGraph.h
src/DijkstraAlgorithmImpl.cpp
src/DijkstraAlgorithmImpl.h
src/Path.cpp
src/Path.h
src/Graph.h
src/ShortestPathAlgorithm.h
src/DijkstraAlgorithm.h)
target_include_directories(DictionaryPath PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
它可以与其他目标(库的客户端(一起查找,但当我试图在同一子目录中添加单元测试时,我遇到了如何定义单元测试目标的问题。例如WordsGraph
类。我定义了一个目标:
add_executable(WordsGraphTest test/WordsGraphTest.cpp)
target_link_libraries(WordsGraphTest GTest::main DictionaryPath)
add_test(NAME WordsGraphTest COMMAND WordsGraphTest)
但如果我尝试引用WordsGraph
头文件,我有:
test/WordsGraphTest.cpp:9:10: fatal error: 'WordsGraph.h' file not found
我理解一个原因——src/
中的文件是私有的,但在这种情况下,如何在没有为链接到它的每个目标实现的情况下测试库内部文件?我应该在每个单元测试中重复编译必要的库文件吗?
add_library(DictionaryPath
...
src/WordsGraph.h
...
)
target_include_directories(DictionaryPath PUBLIC
...
PRIVATE src)
WordsGraph.h
在src
中,您将src
声明为DictionaryPath
的私有包含目录。
如果您不想在创建单元测试时只调用target_link_libraries
,那么您应该将WordsGraph.h
移到include
中,或者将src
声明为公共目录或接口包含目录。
如果您不想将WordsGraph.h
移动到include
中,也不想声明src
为公共目录或接口包含目录,则应该添加对target_include_directories
:的调用
add_executable(WordsGraphTest test/WordsGraphTest.cpp)
target_link_libraries(WordsGraphTest GTest::main DictionaryPath)
target_include_directories(WordsGraphTest PRIVATE src)
add_test(NAME WordsGraphTest COMMAND WordsGraphTest)
解决您遇到的问题应该很容易(找不到WordsGraph.h(。您可以使用include_directions或target_include_directions。
相关文章:
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- VC++本机单元测试,找不到调试符号
- 用于交叉编译和CMake的预处理器宏的单元测试
- C++ 用于单元测试的模板模板
- 提升 1.64 单元测试编译失败
- 单元测试欧拉到四元数实现失败
- 运行 C++ 单元测试时LNK2005链接错误
- 禁用自动捕获 Googletest 单元测试中的C++异常
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 单元测试 - 设置私人成员以获得所需的对象状态
- 通过动态链接和手动设置增强单元测试框架
- 设置单元测试需要多少个项目
- Visual C++项目:最佳单元测试设置
- 在qt单元测试中是否可以用单独的方法设置测试数据?
- CMake:如何设置库的单元测试
- 设置opengl4构建/单元测试服务器
- VS 2013 单元测试不起作用 =设置执行上下文时出现>错误
- 如何单元测试我的类没有任何单元测试框架- visual studio设置