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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With