2017-10-09 14 views
0

私はプロジェクトビルドにCLionとCMakeを使用しています。私は、コンフィギュレーションを構築するGoogleのテストを作成しましたし、私のプロジェクトツリーは次のようになります。トークナイザため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のような)テスト源を配置することができAMまたは私は私自身のテストスクリプトを書く必要がありますか?どうしたらいいですか?

+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フラグを使用できます。変数参照やその他のコンテンツを置き換えずにファイルをコピーします。あなたのtokenizer_test.cppに近いCMakeLists.txtが含まれている必要があります

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

をPS:私はあなたのケースで0.cpptokenizer_test_parse_input.cppのような名前を付ける必要があり、それは良いでしょう、あなたがやっている試験に従って、各ソースファイルと入力ファイルの名前を変更することができ示唆`tokenizer_test.cpp」の近くにこのファイルを配置する。

0

をあなたは他の変数のためのhttps://cmake.org/Wiki/CMake_Useful_Variablesを参照してください。

を呼び出すことができます

ここでは、これを行うプロジェクトのスニペットを紹介します。

全体的に、このスニペットを使用すると、ファイルパスがユニットテストでハードコードされるため、ビルドディレクトリが有効であれば、ファイルシステム上の特定のファイルの場所への依存を避けることができます。

自動化されたビルドとチェックを簡略化します。テストランナーを呼び出す前に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 

テストに使用:

​​

enum image_idsimage_paths配列に読み込み可能なインデックス付けのために使用されています。 IMHO、どの画像が読み込まれたかをはっきりと示しているので、image_paths[0]よりはるかに優れています。

関連する問題