Skip to content

Adding the Catch testing framework

David Pérez-Suárez edited this page Sep 13, 2018 · 4 revisions

Catch is a simple testing framework. This script makes it easy to integrate it with CMake. In its simplest version:

find_packages(Catch 2.3.0 EXACT) # If a particular version is required.
include(AddCatchTest)
enable_testing()
add_catch_test(mytest)

This will download Catch if it not found, create a test, and add it to the CTest framework.

There are several options:

add_catch_test(<name>       # The test will be called test_<name>
   [source1 source2...]     # Sources for the test. Defaults to ``<name>.cc`` if no sources are given.
   [NOMAIN]                 # By default, a standard main is added to the test.
                            # This option specifies that one will be given, either in the sources,
                            # or as a target (object archive) called common_catch_main_object
   [NOTEST]                 # Do not add to CTest (don't run on make test). Many of the options below
                            # will not be useful if NOTEST is enabled.
   [NOCATCHLABEL]           # Do not add ``catch`` as a CTest label
   [SEED number|time]       # Adds a specific seed. Defaults to time.
   [WORKING_DIRECTORY dir]  # Directory from where to launch the test
   [LIBRARIES lib1 lib2...] # Libraries the executable should link against. 
   [DEPENDS dep1 dep2 ...]  # Targets the executable depends on.
   [INCLUDES inc1 inc2...]  # Include directories the executable requires for compilation
   [LABELS lab1 lab2...]    # CTest labels to add to the test.
   [ARGUMENTS arg1 arg2...] # Extra arguments when running the test.
)

If the variable CATCH_JUNIT is defined and true, then ctest will output in Junit format to ${PROJECT_BINARY_DIR}/Testing/<name>.xml.

Additionally, it is possible to re-add a test with a specific seed. This is convenient if a seed is known to give trouble.

add_catch_test_with_seed(<name>  # Name of the test instance
   <exec>                   # Executable of the test, generally test_<name>
   <seed>                   # A number
   [NOCATCHLABEL]           # Do not add ``catch`` as a CTest label
   [WORKING_DIRECTORY dir]  # Directory from where to launch the test
   [LABELS lab1 lab2...]    # CTest labels to add to the test.
   [ARGUMENTS arg1 arg2...] # Extra arguments when running the test.
)

Clone this wiki locally