Catch lib (unit testing) and CTest (CMake) integration
Asked Answered
D

2

25

I'm looking for successful example of Catch CatchLib integration with CMake test (Ctest) . as I understand this is additional cmake script which has to parse application ouput? Did someone already written this? probably shared this?

==================================================

update (solution has been found) :

I've committed cmake script to CatchLib , for the integration Catch with CTest. this is a simplified version of Fraser99's cmake script here

Drumm answered 20/1, 2016 at 10:1 Comment(1)
There is some discussion about CMake in Catch's Build System documentation.Disesteem
P
44

Integrating Catch with CMake is rather simple, as it's a header-only library.

Here's a quick rundown of what you have to do:

You can either assume that the Catch sources are already installed on the build machine or use ExternalProject for fetching them as part of the build process.

In either case, you will end up with the Catch header files in some known directory on your build machine. I would recommend creating an interface target for making this information known to your test executables:

add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${YOUR_CATCH_INCLUDE_DIR})

That way, you can simply specify Catch as a dependency to target_link_libraries:

add_executable(my_test ${MY_TEST_SOURCES})
target_link_libraries(my_test PUBLIC Catch)

As usual with CMake, add_test takes care of introducing the tests to CTest:

enable_testing()
add_test(NAME MyAwesomeTest COMMAND my_test)

And that's it already. Run make test on the built project to run your tests.

I have a project on Github that does this if you need to see a complete working example.

Update for newer versions of Catch: If you've already upgraded to Catch2, that one comes with its own package config file so you can just integrate it calling find_package. This provides a smoother CMake integration overall and you don't have to start defining your own interface target. While the approach above will still work even with Catch2, I would recommend using find_package if your Catch version supports it already.

Peremptory answered 20/1, 2016 at 12:24 Comment(6)
but is there a scripts to add ctest tests automatically?Drumm
Only if you write one. How should CMake know which of your executables are the tests?Peremptory
github.com/Fraser999/MaidSafe/blob/…Drumm
raise a pull request for Catch lib for auto generating CTest definitions for Catch test cases github.com/philsquared/Catch/pull/879 hope this will available after the reviewDrumm
catch has not to be linked... it is a header only library. So why writing a target_link_libraries?Misteach
Is it CMake (2.6), Modern CMake (3.0) or More Modern CMake (3.10) ?Misteach
W
6

Install catch with:

 $ git clone https://github.com/catchorg/Catch2 <catch_src_dir>
 $ mkdir <catch_bin_dir>
 $ cd <catch_bin_dir>
 $ cmake -DBUILD_TESTING:BOOL=FALSE <catch_src_dir>
 $ make
 $ make install

Then do add the following to the CMakeLists.txt:

find_package(Catch2 REQUIRED)
target_link_libraries(tests Catch2::Catch2)

See here.

Weismann answered 26/2, 2017 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.