# Struggling to use VSCode with CMakePresets.json to invoke a test

**URL:** https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667
**Category:** Usage
**Tags:** os:linux, tool:ctest
**Created:** [March 4, 2025, 3:56pm UTC](https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667 "2025-03-04T15:56:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![exoosh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/exoosh/32/4077_2.png) [@exoosh](https://discourse.cmake.org/u/exoosh)
#### Post date: [March 4, 2025, 3:56pm UTC](https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667/1 "2025-03-04T15:56:07Z")

</div>

Hi,

I would like to use the built-in facilities of VSCode to invoke `ctest`. However, I am unable to tell it to step into a given `--test-dir` via `CMakePresets.json`.

And so I get one of two symptoms:

1. if I call `enable_testing()` in the top-level `CMakeLists.txt` it attempts to run tests, including some which I don’t want it to run (defined by dependencies included with `add_subdirectory()`); but more importantly which _cannot_ run
2. if I call `enable_testing()` where the test is defined (a subsubdirectory called `unit-test`) that works when invoked manually as in this case I can pass `--test-dir`, but not via `CMakePresets.json` which seems to go by the assumption that the tests are to be invoked in the directory of the top-level `CMakeLists.txt` … additionally because the respective file gets generated at the wrong directory level, I get a complaint about `DartConfiguration.tcl` missing, which doesn’t seem to be fatal, though

How can I teach VSCode through `CMakePresets.json` where to point `ctest` upon invocation, **without** hardcoding the `--test-dir` option plus its argument in the global VSCode settings?

Thanks in advance for any insights.

PS: the Linux distro is Mint, based upon Ubuntu 24.04 LTS, in case it matters. CMake is the latest from the APT repo of Kitware (3.31).

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 9, 2025, 10:22pm UTC](https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667/2 "2025-03-09T22:22:22Z")

</div>

IDEs should be running `ctest` with the top level build directory as the working directory. If an individual test needs a different working directory, that test’s `WORKING_DIRECTORY` property should be set by the project as needed. You or the IDE should not generally be trying to run `ctest` from a different directory and expect that to be carried through to the test in a way that the test can rely on.

The point of the `ctest --test-dir` option is so that you can run `ctest` from anywhere without having to do a `cd` first. But no tests should be relying on this behavior to allow the caller to change the working directory seen by the test.

---

<div class="post-metadata">

### Author: ![exoosh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/exoosh/32/4077_2.png) [@exoosh](https://discourse.cmake.org/u/exoosh)
#### Post date: [March 10, 2025, 8:53am UTC](https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667/3 "2025-03-10T08:53:12Z")

</div>

Hi, first of all thanks for the answer.

My issue is to limit overall which tests run via [the test presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#test-preset). From the behavior I can deduce that `ctest` when pointed to a directory doesn’t attempt to run anything from any other directory as long as there is no `add_subdirectory()` call in said `CMakeLists.txt`. In our case the respective tests are intentionally confined to said subdirectory, so we can limit test runs to those. That’s the desired behavior from my perspective, but it’s not exposed via test presets, as far as I can see.

---

<div class="post-metadata">

### Author: ![vuko](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vuko/32/5845_2.png) [@vuko](https://discourse.cmake.org/u/vuko)
#### Post date: [October 8, 2025, 11:24pm UTC](https://discourse.cmake.org/t/struggling-to-use-vscode-with-cmakepresets-json-to-invoke-a-test/13667/4 "2025-10-08T23:24:15Z")

</div>

I have the exact same issue. Our build output folder looks like this

- project folder
  - build
    - Lib1
      - CMakeFiles
      - …
      - Lib1.a

    - Lib2
      - CMakeFiles
      - …
      - Lib2.a

    - unit-tests
      - CMakeFiles
      - UnitTest1
        - CMakeFiles
        - UnitTest1 (executable)

      - UnitTest2
        - CMakeFiles
        - UnitTest2 (executable)

      - ….

With presets I can’t get the tests to run because I can’t cd into the correct folder and I can’t tell it where to execute ctest
