捕获库(单元测试)和CTest(CMake)集成 [英] Catch lib (unit testing) and CTest (CMake) integration

查看:246
本文介绍了捕获库(单元测试)和CTest(CMake)集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Catch CatchLib 与CMake测试(Ctest)集成的成功示例.据我了解,这是必须解析应用程序输出的其他cmake脚本? 有人写过这个吗?大概分享了这个吗?

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) :

我已将 cmake脚本提交给CatchLib与CTest集成的Catch.这是Fraser99的cmake脚本的简化版本,此处

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

推荐答案

将Catch与CMake集成非常简单,因为它是仅标头的库.

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:

您可以假定Catch源已经安装在构建计算机上,或者使用

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.

在任何一种情况下,您最终都会在构建计算机上某个已知目录中获得Catch头文件.我建议创建一个接口目标,以使测试可执行文件了解此信息:

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})

这样,您可以简单地将Catch指定为target_link_libraries的依赖项:

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)

与CMake一样, add_test 小心地将测试引入CTest:

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

enable_testing()
add_test(NAME MyAwesomeTest COMMAND my_test)

仅此而已.在已构建的项目上运行make test来运行测试.

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

我在Github上有一个项目,如果您这样做,需要查看完整的工作示例.

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

更新以获取较新版本的Catch::如果您已经升级到Catch2,则该文件将带有其自己的软件包配置文件,因此您可以调用整体上更平滑的CMake集成,不必开始定义自己的接口目标.尽管上面的方法即使在Catch2上仍然可以使用,但如果您的Catch版本已经支持find_package,我建议您使用find_package.

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.

这篇关于捕获库(单元测试)和CTest(CMake)集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆