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

醉酒当歌 提交于 2019-12-02 17:36:24

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 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!