Automated C++ Testing with CTest

CTest is the test driver for CMake. It runs automated tests defined in CMakeLists.txt and integrates with CDash for continuous testing.

Adding Tests

Tests are defined with add_test() in CMake:

add_test(NAME run_test_short
         COMMAND SingleElement --test
         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

Design Considerations

For scientific computing projects, two approaches work well for test modes:

  1. Command-line test flag: A --test runtime option runs a simplified test case within the existing program. Minimal setup but ties test logic to the main executable.

  2. Input-file-based testing: The program reads input files to configure its behaviour. A separate input file for the test case makes the test self-contained, and post-processing scripts (e.g. generate_pvd.py) can be called automatically using the same input file. This is the more maintainable approach.

GitHub Workflows

CTest integrates naturally with GitHub Actions through the cmake --build and ctest commands:

- name: Configure
  run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
- name: Build
  run: cmake --build build
- name: Test
  run: ctest --test-dir build

References