Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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!

like image 750
Teancum Avatar asked Aug 08 '14 19:08

Teancum


1 Answers

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.

like image 135
Juan Leni Avatar answered Oct 05 '22 23:10

Juan Leni