How to do it

We need to inform CTest that tests need to be terminated if they go into overtime, as follows:

  1. We define the project name, enable testing, and define the test:
# set minimum cmake versioncmake_minimum_required(VERSION 3.5 FATAL_ERROR)# project nameproject(recipe-07 LANGUAGES NONE)# detect pythonfind_package(PythonInterp REQUIRED)# define testsenable_testing()# we expect this test to run for 2 secondsadd_test(example ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test.py)
  1. In addition, we specify a TIMEOUT for the test, and set it to 10 seconds:
set_tests_properties(example PROPERTIES TIMEOUT 10)
  1. We know how to configure and build, and we expect the test to pass:
$ ctestTest project /home/user/cmake-recipes/chapter-04/recipe-07/example/build ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.