简体   繁体   English

通过相对路径c ++ cmake guest查找单元测试的外部测试文件

[英]find external test file for unit test by relative path c++ cmake guest

What would be the proper way to access an external test file for the unit test of a c++ project?为 C++ 项目的单元测试访问外部测试文件的正确方法是什么? I am using CMake and Gtest.我正在使用 CMake 和 Gtest。

This is a sample of the directory structure.这是目录结构的示例。

Project
   -src
       -test (unit tests here)
   -test-data (data file here)

Thanks!谢谢!

I prefer to find my test data relative to my executable test. 我更喜欢找到与我的可执行测试相关的测试数据。 To do so, I usually define a helper method in some TestHelpers.h and then pass the relative path of the file I'm looking to resolve. 为此,我通常在一些TestHelpers.h定义一个辅助方法,然后传递我想要解决的文件的相对路径。

inline std::string resolvePath(const std::string &relPath)
{
    namespace fs = std::tr2::sys;
    // or namespace fs = boost::filesystem;
    auto baseDir = fs::current_path();
    while (baseDir.has_parent_path())
    {
        auto combinePath = baseDir / relPath;
        if (fs::exists(combinePath))
        {
            return combinePath.string();
        }
        baseDir = baseDir.parent_path();
    }
    throw std::runtime_error("File not found!");
}

To use it, I go: 要使用它,我去:

std::string foofullPath = resolvePath("test/data/foo.txt");

and that gives me a full path of the test file as long as my executing directory runs from on a descendant of the project's root directory. 只要我的执行目录从项目根目录的后代运行,这就给了我测试文件的完整路径。

Pass the file name to gtest arguments: 将文件名传递给gtest参数:

add_executable(foo ...)
enable_testing()
add_test(FooTest foo "${CMAKE_CURRENT_LIST_DIR}/data/input.file")

get the parameter after gtest parse input: 在gtest解析输入后获取参数:

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  assert(argc == 2); // gtest leaved unparsed arguments for you

and save it to some global *: 并将其保存到某些全局*:

  file_name = argv[1];
  return RUN_ALL_TESTS();

* Usually it's not a very good idea to pollute the global namespace but I think it's fine for testing app *通常,污染全局命名空间并不是一个好主意,但我认为它对测试应用程序很好

Related 有关

您可以使用CMAKE_SOURCE_DIR (给出顶级cmake目录的绝对路径)变量来定义路径并将其传递给测试脚本。

In your CMakefile, add your tests and set some an environment variable with the path to you data. 在CMakefile中,添加测试并设置一些环境变量以及数据路径。

add_test(mytests ${PROJECT_BINARY_DIR}/unittests)
set_tests_properties(mytests PROPERTIES 
                     ENVIRONMENT
                     DATADIR=${CMAKE_CURRENT_SOURCE_DIR}/tests/testvectors)

You can later retrieve the DATADIR from the environment in any test. 您可以稍后在任何测试中从环境中检索DATADIR

You other option is to define a different working directory 您还可以选择定义不同的工作目录

set_tests_properties(mytests PROPERTIES
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)

In my opinion, this is the less intrusive and simpler way. 在我看来,这是一种不那么具有侵入性和简单性的方式。

Just specify a WORKING_DIRECTORY in the add_test function.只需在add_test函数中指定一个WORKING_DIRECTORY Any path specification in the test cases will then be relative to the specified WORKING_DIRECTORY .测试用例中的任何路径规范都将相对于指定的WORKING_DIRECTORY

For example: In your root CMakeLists.txt例如:在您的根 CMakeLists.txt

# ...
add_executable(<name of your test executable> src/test/test.cpp)

# ...

add_test(NAME <arbitrary name> COMMAND <name of your test executable> WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/src/test/")

And then in your google tests use paths like "../test-data/expected_values.csv"然后在你的谷歌测试中使用像"../test-data/expected_values.csv"这样的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM