【发布时间】:2015-12-16 05:00:15
【问题描述】:
我的项目是这样组织的:
- cpp
- main.cpp(从
dataStructures/和common/调用代码) - CMakeLists.txt(最顶层 CMakeLists 文件)
- 构建
- 测试
- main.cpp
- CMakeLists.txt
- 常见
- CMakeLists.txt
- 包括
- 源代码
- 谷歌测试
- 构建
- 常见
- CMakeLists.txt(应该负责构建公共共享库)
- 包括
- utils.h
- 源
- utils.cpp
- 构建
- main.cpp(从
build\ 目录包含构建的目标。实际代码可以看这里:https://github.com/brainydexter/PublicCode/tree/master/cpp
我能够为实际代码构建shared 库(例如common)。现在,我想使用 GoogleTest 对其进行测试。所以,我创建了一个test 目录并将测试代码放在那里(我在 repo 中的只是一个示例,更像是 hello world 让构建系统启动并工作。我稍后会添加更多测试用例)。
我需要一些帮助来从最顶层的 CMakeLists.txt 触发测试。我不知道该怎么做。在test 中,CMakeLists.txt 如下所示:
cmake_minimum_required(VERSION 3.1.2)
add_subdirectory(googletest/googletest)
include_directories(googletest/googletest/include)
add_subdirectory(common)
enable_testing()
add_executable(testCpp main.cpp)
target_link_libraries(testCpp gtest gtest_main )
我是否需要添加任何其他内容才能在 test/common 中启用测试?
test/common/CMakeLists.txt:
file(GLOB SOURCES "src/*.cpp")
add_executable(testCommon ${SOURCES})
target_link_libraries(testCommon cppCommon)
我不确定目标是否应该在此处可执行,或者是否应该是 google 测试框架可以使用的库?我将不胜感激。
【问题讨论】:
-
我不确定我的理解是否正确,但是如果你想测试共享库中的代码,你必须使用这个主要的github.com/brainydexter/PublicCode/blob/master/cpp/test/…创建一个可执行文件,你所有的测试套件并将其链接到您的图书馆。
标签: c++ unit-testing cmake googletest