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

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).

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.

Hi, first of all thanks for the answer.

My issue is to limit overall which tests run via the test presets. 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.