Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working directory for google test in Visual Studio

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:

  1. Set the working directory for the "Test Explorer"
  2. Set the working directory for each test executable
  3. Set the working directory for all google tests
  4. Using CMake set some define that points to a user specified location and use that in the test code. (I consider this a rather ugly solution)

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?

like image 997
Reinhard Hameeteman Avatar asked May 01 '13 14:05

Reinhard Hameeteman


People also ask

How do I add a Google Test to Visual Studio project?

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.

Where is the Visual Studio working directory?

Open Project -> Properties -> Configuration Properties -> Debugging . The working directory entry is $(ProjectDir) by default.

What is the working directory in Visual Studio?

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.


1 Answers

With the current version 0.12.3 of GTA you can at least achieve (1):

  1. Tools
  2. Options
  3. Google Test Adapter (or use Search Options)
  4. General
  5. Working directory (at the bottom)

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.

Screenshot of GTA options dialog

like image 183
Brandlingo Avatar answered Oct 15 '22 10:10

Brandlingo