2017-10-09 19 views
0

프로젝트를 빌드 할 때 CLion과 CMake를 사용하고 있습니다. 나는 구성을 구축 구글 테스트를 만든 내 프로젝트 트리는 다음과 같습니다 토크 나이에 대한Cmake - 출력 폴더를 빌드하기 위해 입력 데이터가있는 파일을 복사하는 방법

project tree

테스트는 간단하다 : 토크 나이는 소스 파일과 출력 토큰을 열어야합니다.

이 tokenizer_test 내 CMakeLists.txt 파일입니다 근처 실행

include_directories(${gtest_SOURCE_DIRS}/include ${gtest_SOURCE_DIRS}) 
add_subdirectory(test_src) 
add_executable(run_tokenizer_tests 
    tokenizer_test.cpp ${CMAKE_SOURCE_DIR}/includes/tokenizer.h 
    ${CMAKE_SOURCE_DIR}/src/tokenizer.cpp 
) 

target_link_libraries(run_tokenizer_tests gtest gtest_main) 

내가 (사진에 0.cpp 같은) 테스트 소스를 배치 할 수 있나요 아니면 내가 내 자신의 테스트 스크립트를 작성해야? 내가 어떻게 해?

+0

[CMake 변수] (https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html)를 살펴보십시오. 테스트에 사용할 수있는 프로젝트 및 소스 디렉토리를 제공하는 몇 가지가 있습니다 (빌드 할 때 매크로로 추가하거나 테스트 실행시 인수로 전달). –

+0

@Someprogrammerdude, unfortunetely, 나는 아무것도 찾지 못했고, 그래서 이것을 썼다. –

+0

@ ДмитрийТерехов 정확히 "0.cpp"에 "테스트 소스"가 무엇을 의미합니까? tokenizer_tests에 대한 데이터를 입력 하시겠습니까? – Liastre

답변

1

플래그와 함께 configure_file CMake 함수를 사용할 수 있습니다. 변수 참조 나 다른 내용을 바꾸지 않고 파일을 복사합니다. 난 당신이 tokenizer_test_parse_input.cpp 같은 이름을 지정해야합니다 귀하의 경우 0.cpp에,하고있는 테스트에 따라 각 소스 파일과 입력 파일의 이름을 변경하는 것이 좋습니다 그것은 더 나은 것 :

configure_file(test_src/0.cpp 0.cpp COPYONLY) 

PS를 : 귀하의 CMakeLists.txttokenizer_test.cpp 근처는 포함해야 `tokenizer_test.cpp "근처에이 파일을 배치합니다.

0

당신은 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_idsimage_paths 어레이 판독 인덱싱을 위해 사용된다. IMHO, 어떤 이미지가 읽혔는지 분명하게 보여주기 때문에 image_paths[0]보다 훨씬 낫습니다.