I have a Visual Studio 2012 C++ solution generated using CMake in which I use google test for unit tests. This works mostly fine, but in one of my tests I want to read a settings file from a local directory. To find the file I copy the file as a post build step from my source code tree to the build and install directory using the following CMake commands:
install(FILES ./adapters/settingFile.txt DESTINATION .)
add_custom_command(TARGET testAdapters POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_SOURCE_DIR}/adapters/settingFile.txt"
"${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Copying elastix parameter files")
This works fine: after building my test the settingFile.txt is in the same location as the testAdapters.exe. Using a right click on the testAdapters project and starting a Debug session also works find.
However if I choose to run the test from within the "Test Explorer" window, either by "Run All" or by right clicking the test and choosing "Run selected tests", the test cannot find settingsFile.txt. By right clicking and choosing "Debug selected tests" I found that running the test from the "Test Explorer" the working directory defaults to the visual studio program directory: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
. I can think of several possible solutions, but don't know how to achieve this:
I need a solution that is platform independent. Does anyone know how to achieve (1) or (2) or do you know of a better solution?
Add a Google Test project in Visual Studio 2019In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.
Open Project -> Properties -> Configuration Properties -> Debugging . The working directory entry is $(ProjectDir) by default.
Working Directory is the location which Specifies the working directory of the program being debugged. It is the default place in which a program is looking up it's files. Normally the working directory is the directory the application is launched from \bin\debug by default.
With the current version 0.12.3 of GTA you can at least achieve (1):
Unfortunately GTA seems to only support $(ExecutableDir)
(the default) and $(SolutionDir)
. It seems that GTA cannot tell which project is the unit test project, so it is not possible to use the project directory as a start directory.
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