Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch lib (unit testing) and CTest (CMake) integration

Tags:

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

like image 499
amigo421 Avatar asked Jan 20 '16 10:01

amigo421


People also ask

What is CMake and CTest?

The ctest executable is the CMake test driver program. CMake-generated build trees created for projects that use the enable_testing() and add_test() commands have testing support. This program will run the tests and report results.

Is CTest part of CMake?

CTest is an executable that comes with CMake; it handles running the tests for the project. While CTest works well with CMake, you do not have to use CMake in order to use CTest.

How do I use CMake on Catch2?

CMake targetsIf you need custom main , you should link only against Catch2::Catch2 . These targets are also provided when Catch2 is used as a subdirectory. Assuming Catch2 has been cloned to lib/Catch2 , you only need to replace the find_package call with add_subdirectory(lib/Catch2) and the snippet above still works.

What is Enable_testing in CMake?

enable_testing() Enables testing for this directory and below. This command should be in the source directory root because ctest expects to find a test file in the build directory root. This command is automatically invoked when the CTest module is included, except if the BUILD_TESTING option is turned off.


2 Answers

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.

like image 129
ComicSansMS Avatar answered Sep 29 '22 19:09

ComicSansMS


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.

like image 24
ricab Avatar answered Sep 29 '22 20:09

ricab