Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CMake单元结构,包含多个测试_C++_Unit Testing_Cmake - Fatal编程技术网

C++ CMake单元结构,包含多个测试

C++ CMake单元结构,包含多个测试,c++,unit-testing,cmake,C++,Unit Testing,Cmake,我想将cmake设置为在具有单元测试的项目上运行。项目结构如下: |---CMakeLists.txt |---build |---source | |---CMakeLists.txt | |---many .cpp and .h files |---tests | |---CMakeLists.txt | |---many .cpp files cmake_minimum_required(VERSION 2.8.11) set(CMAKE_CXX_FLAGS "-std

我想将cmake设置为在具有单元测试的项目上运行。项目结构如下:

|---CMakeLists.txt
|---build
|---source
|   |---CMakeLists.txt
|   |---many .cpp and .h files
|---tests
|   |---CMakeLists.txt
|   |---many .cpp files
cmake_minimum_required(VERSION 2.8.11)

set(CMAKE_CXX_FLAGS "-std=c++11")

project(MyProject CXX)
add_subdirectory(source)
add_subdirectory(tests)
enable_testing()
tests
中的源文件使用Boost
unit\u test.h

我的外部
CMakeLists.txt
如下所示:

|---CMakeLists.txt
|---build
|---source
|   |---CMakeLists.txt
|   |---many .cpp and .h files
|---tests
|   |---CMakeLists.txt
|   |---many .cpp files
cmake_minimum_required(VERSION 2.8.11)

set(CMAKE_CXX_FLAGS "-std=c++11")

project(MyProject CXX)
add_subdirectory(source)
add_subdirectory(tests)
enable_testing()
source
文件夹中的
CMakeLists.txt
是:

file( GLOB LIB_SOURCES *.cpp )
file( GLOB LIB_HEADERS *.h )
add_library( MyProject_lib ${LIB_SOURCES} ${LIB_HEADERS} )
# require old policy to allow for source files containing the name "test"
cmake_policy(SET CMP0037 OLD)
# Locate Boost libraries: unit_test_framework, date_time and regex
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework date_time regex)

include_directories("${CMAKE_SOURCE_DIR}/source" ${Boost_INCLUDE_DIRS})

file(GLOB APP_SOURCES *.cpp)
foreach(testsourcefile ${APP_SOURCES})
    string(REPLACE ".cpp" "" testname ${testsourcefile})
    add_executable(${testname} ${testsourcefile})
    target_link_libraries(${testname} LINK_PUBLIC ${Boost_LIBRARIES} MyProject_lib)
    add_custom_target(targetname)
    add_custom_command(TARGET targetname POST_BUILD COMMAND ${testname})
endforeach(testsourcefile ${APP_SOURCES})
tests
文件夹中的
CMakeLists.txt
是:

file( GLOB LIB_SOURCES *.cpp )
file( GLOB LIB_HEADERS *.h )
add_library( MyProject_lib ${LIB_SOURCES} ${LIB_HEADERS} )
# require old policy to allow for source files containing the name "test"
cmake_policy(SET CMP0037 OLD)
# Locate Boost libraries: unit_test_framework, date_time and regex
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.59 REQUIRED COMPONENTS unit_test_framework date_time regex)

include_directories("${CMAKE_SOURCE_DIR}/source" ${Boost_INCLUDE_DIRS})

file(GLOB APP_SOURCES *.cpp)
foreach(testsourcefile ${APP_SOURCES})
    string(REPLACE ".cpp" "" testname ${testsourcefile})
    add_executable(${testname} ${testsourcefile})
    target_link_libraries(${testname} LINK_PUBLIC ${Boost_LIBRARIES} MyProject_lib)
    add_custom_target(targetname)
    add_custom_command(TARGET targetname POST_BUILD COMMAND ${testname})
endforeach(testsourcefile ${APP_SOURCES})
如果我用
add_custom_target
add_custom_command
注释行,此配置的结果是正确构建和链接了库,但没有编译任何测试

如果我没有用
add_custom_target
add_custom_command
注释这两行,那么运行
cmake
得到的错误是:

CMake Error at tests/CMakeLists.txt:25 (add_custom_target):
add_custom_target cannot create target "targetname" because another target
with the same name already exists.  The existing target is a custom target
created in source directory "/tests".  See
documentation for policy CMP0002 for more details.

因为我有大量的测试,所以逐个列出它们是不实际的。我想让cmake找到所有的.cpp文件,就像在源文件夹中找到库一样,编译并运行它们。

这是我在库中使用的解决方案:

FILE( GLOB log_testprograms *.cpp )

FOREACH( testsource ${log_testprograms} )
   GET_FILENAME_COMPONENT( filename ${testsource} NAME_WE )
   celma_add_celma_boost_testprogram( ${filename} )
ENDFOREACH()
celma\u add\u celma\u boost\u testprogram
是这样一个宏:

macro( celma_add_celma_boost_testprogram  filename )
   add_executable(        ${filename}  ${filename}.cpp )
   target_link_libraries( ${filename}  celma ${Boost_Test_Link_Libs} )
   add_test(              ${filename}  ${CMAKE_CURRENT_BINARY_DIR}/${filename} )
endmacro( celma_add_celma_boost_testprogram )

Celma是我的库的名称,变量
Boost\u Test\u Link\u Libs
包含要链接的Boost库的列表,包括Boost.Test库。

谢谢,这确实解决了编译
tests
文件夹中源文件的问题,但是在编译和链接可执行文件之后如何运行它们呢?当您使用
add_test
命令时,您可以在之后简单地键入
make test
来运行所有测试程序。如果我键入
make test
,我将收到警告
未找到任何测试。如果我在宏中添加以下行:
add_custom_command(TARGET${filename}POST_BUILD command${filename})
,那么它可以工作,但我认为这不是使用cmake进行单元测试的正确方法。我发布的这几行应该足够了。也许您应该在
add\u子目录
调用之前移动
enable\u testing
调用。