Question

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 ;
Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top