By default CMake’s test target doesn’t depend on all which leaves many users scratching their heads when ‘make test’ fails because the project isn’t built yet.
We can use set(CMAKE_SKIP_TEST_ALL_DEPENDENCY FALSE) to fix this, but we need to paste this set in every top-level CMakeLists.txt that’s attempting to follow best practices.
Is there appetite to switch the default behavior to have test depend on all via a policy change?
test not depending on all has been the convention since CMake started. It is considered a convenience for running ctest, and running ctest directly would not build all.
CMAKE_SKIP_TEST_ALL_DEPENDENCY was added in CMake 3.29 as an option.
As maintainer, I’m not particularly interested in changing the default that has been established for over 20 years.
To pick up on another aspect of this, I’d recommend moving away from using the test build target at all and instead execute ctest directly. Most mainstream IDEs are doing so these days, and it offers a far richer and more flexible set of capabilities, especially for developers working directly from a command line. The days of make test stem from an era that lacked much of today’s testing features (I’m not just talking about CMake projects in that statement).
… but the error messages are note really helpfully:
bash-5.2$ ctest --preset dev
Test project /Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev
Start 1: find-package-test
Start 2: header-only-test
Could not find executable /Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Looked in the following places:
/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/Debug/header-only-test
/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/Debug/header-only-test
Debug//Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Debug//Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/Debug/header-only-test
Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/Debug/header-only-test
Debug/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Debug/Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
Unable to find executable: /Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/header-only-test
1/3 Test #2: header-only-test .................***Not Run 0.00 sec
Start 3: module_header-only-test
2/3 Test #3: module_header-only-test .......... Passed 0.03 sec
3/3 Test #1: find-package-test ................***Failed 1.00 sec
Internal cmake changing into directory: /Users/clausklein/Workspace/cpp/cxx20/fmt-module/build/dev/tests/find-package-test
Error: cmake execution failed
The CXX compiler identification is Clang 19.1.4
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working CXX compiler: /usr/local/opt/llvm/bin/clang++ - skipped
Detecting CXX compile features
Detecting CXX compile features - done
CMake Error at CMakeLists.txt:9 (find_package):
By not providing "Findfmt.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "fmt", but
CMake did not find one.
Could not find a package configuration file provided by "fmt" (requested
version 11.0.2.1) with any of the following names:
fmtConfig.cmake
fmt-config.cmake
Add the installation prefix of "fmt" to CMAKE_PREFIX_PATH or set "fmt_DIR"
to a directory containing one of the above files. If "fmt" provides a
separate development package or SDK, be sure it has been installed.
Configuring incomplete, errors occurred!
33% tests passed, 2 tests failed out of 3
Total Test time (real) = 1.00 sec
The following tests FAILED:
1 - find-package-test (Failed)
2 - header-only-test (Not Run)
Errors while running CTest
bash-5.2$