# Proper CTest integration with Visual Studio?

**URL:** https://discourse.cmake.org/t/proper-ctest-integration-with-visual-studio/10042
**Category:** Code
**Tags:** gen:vs
**Created:** [February 9, 2024, 1:41pm UTC](https://discourse.cmake.org/t/proper-ctest-integration-with-visual-studio/10042 "2024-02-09T13:41:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [February 9, 2024, 1:41pm UTC](https://discourse.cmake.org/t/proper-ctest-integration-with-visual-studio/10042/1 "2024-02-09T13:41:02Z")

</div>

I’d like to tightly integrate my test suite with Visual Studio 2019. If I understand correctly, the correct way is to reference one project to another:

- [Write unit tests for C++ DLLs - Visual Studio (Windows) | Microsoft Learn](https://learn.microsoft.com/en-us/visualstudio/test/walkthrough-writing-unit-tests-for-cpp-dlls?view=vs-2019#make_functions_visible)

So how would one do equivalent using cmake ? For the example let’s consider the simple setup:

```auto
> cat .\src\CMakeLists.txt
add_library(foo SHARED foo.c foo.h)
target_include_directories(
  foo PUBLIC $<BUILD_INTERFACE:${p_SOURCE_DIR}/src>
)

```

and tests directory is:

```auto
> cat .\tests\CMakeLists.txt
add_executable(footest footest.c)
target_link_libraries(footest PRIVATE foo)
add_test(NAME footest COMMAND footest)

```

By default `footest.exe` will fail to execute since `foo.dll` will be located in a different directory.

Please no suggestion about tweaking the `PATH` in a `set_tests_properties` call or putting all \*.dll inside a single `CMAKE_RUNTIME_OUTPUT_DIRECTORY `. I’d really like to reference projects so that Visual Studio copies the Debug/Release/… dll for me (I’d prefer a solution without an explicit copy in a POST\_BUILD custom command).

Currently:

 ![image](https://discourse.cmake.org/uploads/default/original/2X/5/5434a66398eaf429fa7b109c7a703fe36d1790ac.png)

with:

![image](https://discourse.cmake.org/uploads/default/original/2X/6/6866619291c4f8680b6e6e45c3fe8bcfe836a100.png)

Thanks

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 17, 2024, 5:57am UTC](https://discourse.cmake.org/t/proper-ctest-integration-with-visual-studio/10042/2 "2024-02-17T05:57:25Z")

</div>

You can direct where binaries go by setting [`CMAKE_RUNTIME_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_RUNTIME_OUTPUT_DIRECTORY.html) at the top-level. Then all `.exe` and `.dll` files will live beside each other. This doesn’t help with third party DLLs though; see [`$<TARGET_RUNTIME_DLLS>`](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#genex:TARGET_RUNTIME_DLLS). for that (if all relevant deps are represented as CMake targets).
