您可以撥打file(COPY source DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
其他變量見https://cmake.org/Wiki/CMake_Useful_Variables。
這是我們項目的一小部分,它可以做到這一點,還有更多。
總體來看,這段代碼可以讓你避免在文件系統上的特定文件位置的依存關係,只要你的build目錄是活的,因爲文件路徑會在你的單元測試進行硬編碼。
它簡化自動化生成和檢查 - 無需調用測試跑步之前跟蹤到哪裏cd
。
這也提高了單元測試的代碼的可讀性。
CMakeLists.txt
:
# list all test images
set(test_data
orig_5_15Fps_3_27.png
orig_5_15Fps_5_34.png
....
)
# Loop over all items in the "test_data" list
# Copy PNG, JPEG and BMP images to the directory, where test binaries are created
# And generate full paths to them for hardcoding in the include file test_config.h
foreach(df ${test_data})
# copy images to build dir
if(${df} MATCHES "((jp|pn)g|bmp)$")
file(COPY ${df} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
set(df_file_path ${CMAKE_CURRENT_BINARY_DIR}/${df})
else()
set(df_file_path ${CMAKE_CURRENT_SOURCE_DIR}/${df})
endif()
# generate some C++ code in CMake variables IMAGE_PATHS and IMAGE_IDS
# (see below)
if (NOT IMAGE_PATHS)
set(IMAGE_PATHS " \"${df_file_path}\"")
else()
set(IMAGE_PATHS "${IMAGE_PATHS},\n \"${df_file_path}\"")
endif()
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" df_id ${df})
if (NOT IMAGE_IDS)
set(IMAGE_IDS " img_${df_id}")
else()
set(IMAGE_IDS "${IMAGE_IDS},\n img_${df_id}")
endif()
endforeach()
set(TEST_PATH \"${CMAKE_CURRENT_BINARY_DIR}\")
configure_file(test_config.h.in test_config.h @ONLY) # see below for test_config.h.in
...
include_directories(${CMAKE_CURRENT_BINARY_DIR}) # make test_config.h visible for compiler
add_executable (some_unit_test some_unit_test.cpp)
add_test(NAME some_unit_test COMMAND some_unit_test WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
...
add_executable (...)
add_test( ...)
...
文件test_config.h.in
。
/* DO NOT EDIT THIS FILE, IT IS AUTOGENERATED!
All your changes will be overwritten.
If you want to add new test data files,
add them to the `test_data` list in file CMakeLists.txt
*/
#ifndef __TEST_CONFIG_H__
#define __TEST_CONFIG_H__
const char* test_path = @[email protected]; //!< full path to test data, without trailing slash
//! full paths to all test images
const char* image_paths[] = {
@[email protected]
};
enum image_ids { //!< test file names, converted to enum constants
@[email protected]
};
#endif
電話configure_file
取代@[email protected]
,@[email protected]
和@[email protected]
與他們的價值觀。
這裏是build目錄文件test_config.h
配置方式中的模樣。
/* DO NOT EDIT THIS FILE, IT IS AUTOGENERATED!
All your changes will be overwritten.
If you want to add new test data files,
add them to the `test_data` list in file CMakeLists.txt
*/
#ifndef __TEST_CONFIG_H__
#define __TEST_CONFIG_H__
const char* test_path = "F:/projects/project/build64/test"; //!< full path to test data, without trailing slash
//! full paths to all test images
const char* image_paths[] = {
"F:/projects/project/build64/test/orig_5_15Fps_3_27.png",
"F:/projects/project/build64/test/orig_5_15Fps_5_34.png",
...
};
enum image_ids { //!< test file names, converted to enum constants
img_orig_5_15Fps_3_27_png,
img_orig_5_15Fps_5_34_png,
...
};
#endif
使用在測試:
enum image_ids
用於image_paths
陣列中讀取索引。恕我直言,它比image_paths[0]
好得多,因爲它清楚地顯示了讀取哪個圖像。
看一看[CMake變量](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html)。有幾個爲您提供可用於測試的項目和源代碼目錄(通過在構建時將它們添加爲宏,或在運行測試時將它們作爲參數傳遞)。 –
@Someprogrammerdude,不幸的是,我找不到,所以我寫了這個。 –
@ДмитрийТерехов「測試來源」是什麼意思「0.cpp」包含什麼?爲tokenizer_tests輸入數據? – Liastre