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

僤鯓⒐⒋嵵緔 提交于 2019-12-10 02:16:06

问题


What would be the proper way to access an external test file for the unit test of a c++ project? I am using CMake and Gtest.

This is a sample of the directory structure.

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

Thanks!


回答1:


Pass the file name to gtest arguments:

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

get the parameter after gtest parse input:

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

  • How to pass parameters to the gtest



回答2:


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.

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.




回答3:


You could use the CMAKE_SOURCE_DIR (gives absolute path of the top level cmake directory) variable to define the path and pass it to the test script.




回答4:


In your CMakefile, add your tests and set some an environment variable with the path to you data.

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.

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.



来源:https://stackoverflow.com/questions/25211110/find-external-test-file-for-unit-test-by-relative-path-c-cmake-guest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!