당신은 file(COPY source DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
다른 변수 https://cmake.org/Wiki/CMake_Useful_Variables를 참조 호출 할 수 있습니다.
,691을
여기에 우리 프로젝트의 스 니펫이 나와 있습니다.
전반적으로이 스 니펫을 사용하면 파일 경로가 유닛 테스트에서 하드 코드되기 때문에 파일 디렉토리의 특정 파일 위치에 대한 종속성을 피할 수 있습니다.
자동화 된 빌드 및 점검을 단순화하여 테스트 러너를 호출하기 전에 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]
을 대체합니다.
다음은 빌드 디렉토리에 구성된 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
시험에 사용 :
#include "test_config.h"
....
img0 = cv::imread(image_paths[img_orig_5_15Fps_3_27_png], cv::IMREAD_GRAYSCALE);
enum image_ids
는 image_paths
어레이 판독 인덱싱을 위해 사용된다. IMHO, 어떤 이미지가 읽혔는지 분명하게 보여주기 때문에 image_paths[0]
보다 훨씬 낫습니다.
[CMake 변수] (https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html)를 살펴보십시오. 테스트에 사용할 수있는 프로젝트 및 소스 디렉토리를 제공하는 몇 가지가 있습니다 (빌드 할 때 매크로로 추가하거나 테스트 실행시 인수로 전달). –
@Someprogrammerdude, unfortunetely, 나는 아무것도 찾지 못했고, 그래서 이것을 썼다. –
@ ДмитрийТерехов 정확히 "0.cpp"에 "테스트 소스"가 무엇을 의미합니까? tokenizer_tests에 대한 데이터를 입력 하시겠습니까? – Liastre