如何在基于 CMake 的项目中使用 Boost.Test?

发布于 2024-10-05 09:24:05 字数 313 浏览 11 评论 0原文

我的项目使用 CMake 作为其构建系统,我希望它执行我的 Boost.Test 测试用例。

我怎样才能做到这一点?在 Boost.Build 中,我可以这样做:

import testing ;

use-project /my_lib : ../src ;

unit-test my_test
          : my_test.cpp
            /my_lib
          boost_unit_test_framework
        ;

lib boost_unit_test_framework ;

My project uses CMake as its build system, and I want it to execute my Boost.Test test cases.

How can I achieve that? In Boost.Build, I could do it as follows:

import testing ;

use-project /my_lib : ../src ;

unit-test my_test
          : my_test.cpp
            /my_lib
          boost_unit_test_framework
        ;

lib boost_unit_test_framework ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮了一弯 2024-10-12 09:24:05

CMake 本身只是一个构建系统; CTest 是一个与 CMake 集成的测试运行器。两者都不是单元测试框架;这项工作可以通过 Boost.Test 或 googletest 来完成。

要在 CMake 项目中使用基于 Boost.Test 的单元测试程序,您首先需要使用 CMake 构建并链接您的单元测试二进制文件,并在您的项目中使用 add_executabletarget_link_libraries CMakeLists.txt 脚本。然后,您可以将单元测试二进制文件添加到 CTest 的测试列表中,以便使用 enable_testingadd_test 运行。

如果您想要真正喜欢,您可以查看 CMake 文档,了解如何让 CMake 搜索所有源文件以自动查找和构建单元测试,但首先要做的是......

CMake itself is just a build system; CTest is a just test runner that is integrated with CMake. Neither is a unit test framework; that job can be done by Boost.Test or googletest.

To use a Boost.Test-based unit test program in a CMake project, you'd first have CMake build and link your unit test binary, using add_executable and target_link_libraries in your CMakeLists.txt script. Then, you can add the unit test binary to the list of tests for CTest to run with enable_testing and add_test.

If you want to get really fancy, you can look through the CMake documentation for how to have CMake search through all your source files to find and build unit tests automatically, but first things first...

渡你暖光 2024-10-12 09:24:05

我在 https://github.com/rpavlik/cmake-modules/ 包括一些用于集成升压测试的内容 - 请参阅该存储库中的自述文件,了解有关使用它们的最简单方法的信息。

然后,您需要执行以下操作,假设 test_DimensionedQuantities.cpp 是 boost.test 测试驱动程序源。

include(BoostTestTargets)
add_boost_test(DimensionedQuantities
 SOURCES
 test_DimensionedQuantities.cpp)

这仅添加了一个 CTest 可见测试,如果任何 boost 测试失败,该测试就会失败。如果您有可以通过名称指定给测试驱动程序的测试(最简单的宏属于此类),您可以执行以下操作:

include(BoostTestTargets)
add_boost_test(DimensionedQuantities
 SOURCES
 test_DimensionedQuantities.cpp
 TESTS
 CheckCollision
 BodyPoseNotCorrupted
 CheckGraspTransform
 BodyFollowsMockManip1D
 BodyFollowsMockManip2D
 BodyFollowsMockManip3D)

还有更多选项,包括配置标头以选择最佳选项:包含的 UTF 版本、b:静态链接或 c:动态链接,以及针对库的链接等。只需在 cmake 文件中查找信息即可。

I've made some modules at https://github.com/rpavlik/cmake-modules/ including some for integrating boost test - see the readme in that repo for info on the easiest way to use them.

Then, you'd want to do the following, assuming test_DimensionedQuantities.cpp is a boost.test test driver source.

include(BoostTestTargets)
add_boost_test(DimensionedQuantities
 SOURCES
 test_DimensionedQuantities.cpp)

This adds just a single CTest-visible test that fails if any of the boost tests fail. If you have tests that can be specified by name to the test driver (the simplest macros fall in this category), you can do something like this:

include(BoostTestTargets)
add_boost_test(DimensionedQuantities
 SOURCES
 test_DimensionedQuantities.cpp
 TESTS
 CheckCollision
 BodyPoseNotCorrupted
 CheckGraspTransform
 BodyFollowsMockManip1D
 BodyFollowsMockManip2D
 BodyFollowsMockManip3D)

There are a bunch more options, including configuring a header to choose the best option of a: included version of UTF, b: static link, or c: dynamic link, as well as linking against libraries, etc. Just look in the cmake file for info.

↙厌世 2024-10-12 09:24:05

请参阅 CMake 文档/书籍中的 CMake 测试项目和/或 CTest 内容。

See the CMake test projects and/or the CTest stuff in the CMake documentation/book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文