Clients of the message library are now very happy since they can finally install the library on their system and have CMake discover it for them with minimal modifications to their own CMakeLists.txt:
find_package(message VERSION 1 REQUIRED)
Clients can now configure their project with the following:
$ cmake -Dmessage_DIR=/path/to/message/share/cmake/message ..
The tests included with our example show how to check that the installation of the targets went according to plan. Looking at the structure of the tests folder we notice the use_target subdirectory:
tests/
├── CMakeLists.txt
└── use_target
├── CMakeLists.txt
└── use_message.cpp
This directory contains a small project that uses the exported targets. The interesting part is in the CMakeLists.txt file specifying the tests:
- We test that the small project can be configured to use the installed library. This is the set up step of the use-target test fixture, as shown in Chapter 4, Creating and Running Tests, Recipe 10, Using test fixtures:
add_test(
NAME use-target_configure
COMMAND
${CMAKE_COMMAND} -H${CMAKE_CURRENT_LIST_DIR}/use_target
-B${CMAKE_CURRENT_BINARY_DIR}/build_use-target
-G${CMAKE_GENERATOR}
-Dmessage_DIR=${CMAKE_INSTALL_PREFIX}/${
INSTALL_CMAKEDIR}
-DCMAKE_BUILD_TYPE=$<CONFIGURATION>
)
set_tests_properties(use-target_configure
PROPERTIES
FIXTURES_SETUP use-target
)
- We test that the small project can be built:
add_test(
NAME use-target_build
COMMAND
${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/build_use-target
--config $<CONFIGURATION>
)
set_tests_properties(use-target_build
PROPERTIES
FIXTURES_REQUIRED use-target
)
- The tests of the small projects are also run:
set(_test_target)
if(MSVC)
set(_test_target "RUN_TESTS")
else()
set(_test_target "test")
endif()
add_test(
NAME use-target_test
COMMAND
${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/build_use-target
--target ${_test_target}
--config $<CONFIGURATION>
)
set_tests_properties(use-target_test
PROPERTIES
FIXTURES_REQUIRED use-target
)
unset(_test_target)
- Finally, we tear down the fixture:
add_test(
NAME use-target_cleanup
COMMAND
${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/build_use-target
)
set_tests_properties(use-target_cleanup
PROPERTIES
FIXTURES_CLEANUP use-target
)
Note that these tests can only be run after the project has been installed.