Reputation: 2759
Some CMake projects add conditional support for generating projects with unit test support. The pattern followed in the project's root CMakeLists.txt
file goes something like:
cmake_minimum_required(VERSION 3.0)
(...)
# add subdirectories, libraries, executables, etc
(...)
option(ENABLE_TESTS "Generate project with unit test support" ON)
if(ENABLE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
Although this pattern is rather common, I've also noticed that the names picked for the unit tests option is somewhat arbitrary. Although the option BUILD_TESTING
was introduced in cmake v3.15, that name was far from standard prior to that release. So far I've noticed projects using the following option names:
BUILD_TESTS
BUILD_TESTING
ENABLE_TESTS
RUN_TESTS
With this in mind, does anyone know if there's a standard naming convention to control whether unit tests are added or not to a CMake project?
Upvotes: 3
Views: 923
Reputation: 140960
if there's a standard naming convention to control whether unit tests are added or not to a CMake project?
I usually see the pattern like:
if (YOUR_LIBRARY_BUILD_TESTING AND BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
endif()
That way:
-DYOUR_LIBRARY_BUILD_TESTING=1 -DBUILD_TESTING=1
.-DHIS_LIBRARY_BUILD_TESTING=1 -DBUILD_TESTING=1
BUILD_TESTING
around add_subdirectory
like set(sav ${BUILD_TESTING}) set(BUILD_TESTING 0) add_subdirectory(your_library) set(BUILD_TESTING ${sav})
, which looks ugly. And in case you want to test the dependent library (ex. with specific different toolchain the upstream doesn't test with) you have to edit cmake files.-DBUILD_TESTING=0
to overall disable all possible tests.Modern cmake, I believe currently the cmake
tutorial, recommends:
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MYPROJECT_BUILD_TESTING) AND BUILD_TESTING)
which only just let's you omit setting MYPROJECT_BUILD_TESTING=1
when testing your own project.
Some short research on kitware projects, I see VTK_BUILD_TESTING, PARAVIEW_BUILD_TESTING, VTKm_ENABLE_TESTING, SMTK_ENABLE_TESTING, and even ${PROJECT_NAME}_BUILD_TESTING. Some seem to forcible set BUILD_TESTING
to ON
when project specific *_BUILD_TESTING
is enabled.
So far I've noticed projects using the following option names
Everyone comes and uses hes own convention, I believe this is the reason cmake was actually adopted that well - it doesn't "force" any specific thing. I believe most developers when writing cmake scripts are not aware of BUILD_TESTING
variable yet, so they start coming up with their own convention - or maybe BUILD_TESTING
is the wrong name, as it only means that tests are going to be build, not run. Anyway, as BUILD_TESTING
is with cmake from a loooong time, I recommend naming project specific flag as a *_BUILD_TESTING
.
Upvotes: 1