15

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 ;
forneo
  • 715
  • 1
  • 8
  • 13

3 Answers3

14

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

Ben Karel
  • 4,341
  • 1
  • 24
  • 25
  • +1 for this info. Could you perhaps explain how to [automatically build unit tests](http://stackoverflow.com/q/16857517/819272)? – TemplateRex May 31 '13 at 17:04
11

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.

Ryan Pavlik
  • 1,607
  • 1
  • 11
  • 20
-4

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

Edward Strange
  • 38,861
  • 7
  • 65
  • 123
  • That seems to be a different testing framework, is there no way to use Boost.Test? – forneo Nov 28 '10 at 22:52
  • The correct answer is that you should be using CTest. If you do then you'll be able to `make test` and/or use ctest, with/without the dashboard, to build and run any program you want. Again, look in the CMake documentation. – Edward Strange Nov 28 '10 at 23:00