2017-10-09 92 views
0

我使用的克利翁和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,不幸的是,我找不到,所以我寫了這個。 –

+0

@ДмитрийТерехов「測試來源」是什麼意思「0.cpp」包含什麼?爲tokenizer_tests輸入數據? – Liastre

回答

1

可以使用configure_file CMake的功能,與COPYONLY標誌。它將複製文件而不替換任何變量引用或其他內容。你CMakeLists.txt附近tokenizer_test.cpp應包含以下內容:

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

PS:我建議你根據測試你在幹什麼,你的情況0.cpp名稱應該像tokenizer_test_parse_input.cpp重命名每一個源文件並輸入文件,它會更好這個文件放在靠近`tokenizer_test.cpp」。

0

您可以撥打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]好得多,因爲它清楚地顯示了讀取哪個圖像。

相關問題