简体   繁体   English

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

[英]How can I use Boost.Test in a CMake based project?

My project uses CMake as its build system, and I want it to execute my Boost.Test test cases. 我的项目使用CMake作为其构建系统,我希望它能够执行我的Boost.Test测试用例。

How can I achieve that? 我怎样才能做到这一点? In Boost.Build, I could do it as follows: 在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 ;

CMake itself is just a build system; CMake本身只是一个构建系统; CTest is a just test runner that is integrated with CMake. CTest是一个与CMake集成的测试运行器。 Neither is a unit test framework; 也不是单元测试框架; that job can be done by Boost.Test or googletest. 这项工作可以通过Boost.Test或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. 要在CMake的项目使用基于如Boost.Test单元测试程序,你首先必须建立的CMake和链接单元测试二进制文件,使用add_executabletarget_link_libraries在你CMakeLists.txt脚本。 Then, you can add the unit test binary to the list of tests for CTest to run with enable_testing and add_test . 然后,您可以将单元测试二进制文件添加到CTest的测试列表中,以使用enable_testingadd_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... 如果你想变得非常花哨,你可以查看CMake文档,了解如何通过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. 我在https://github.com/rpavlik/cmake-modules/上制作了一些模块,包括一些用于集成boost测试的模块 - 请参阅该repo中的自述文件,了解最简单的使用方法。

Then, you'd want to do the following, assuming test_DimensionedQuantities.cpp is a boost.test test driver source. 然后,假设test_DimensionedQuantities.cpp是一个boost.test测试驱动程序源,您需要执行以下操作。

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. 如果任何增强测试失败,这只会增加一个CTest可见测试失败。 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. 还有更多选项,包括配置标头以选择最佳选项:包含的UTF版本,b:静态链接,或c:动态链接,以及链接库等。只需查看cmake文件信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM