Skip to content

Instantly share code, notes, and snippets.

@FerryT
Last active August 24, 2019 11:11
Show Gist options
  • Save FerryT/7eb71f8c7c26159a904cf7fd99123f63 to your computer and use it in GitHub Desktop.
Save FerryT/7eb71f8c7c26159a904cf7fd99123f63 to your computer and use it in GitHub Desktop.
CMake tests that are build only when tests are run
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyProject)
add_executable(main src/main.cpp)
enable_testing()
# add a build target for the test executable
add_executable(testing src/testing.cpp)
# configure the target so it does not compile by default
set_target_properties(testing PROPERTIES EXCLUDE_FROM_ALL TRUE)
# add a test that actually compiles the text executable
# if this test fails, the test executable could not be compiled
# explicitly compile the target (`make testing` in this case) to see what failed
add_test(NAME TestExecutable COMMAND "${CMAKE_COMMAND}"
--build "${CMAKE_BINARY_DIR}"
--config "$<CONFIG>"
--target "testing"
)
# add some test
add_test(Test1 testing)
# making sure it depends on the text executable, so it will be build
# before the test is run.
set_tests_properties(Test1 PROPERTIES DEPENDS TestExecutable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment