开发者

cmake/ctest: is it possible to use the creation of build targets as tests?

I have a program which reads a description of a file format (ISO 10303-11 for the curious) and generates c++ from it. The generated code is compiled into a library, and the library is linked with various executables to read/write that fil开发者_开发技巧e format.

I have CMake set up to build the generator program and generate libraries for whichever file formats are specified at configuration time.

Now, I want to add tests. These tests are identical to steps that are already done in the build process - must I redo these steps in a CTest script, or is there a way to tell ctest to build a target and capture any error messages?


If you are using a recent enough version of ctest (most recent official release is 2.8.6) then you can use the NAME/COMMAND signature of add_test, in conjunction with the cmake --build command line option, to build specific targets in your build tree.

For example, the following should work, assuming a custom target (or library or executable target) named "tgt1":

add_test(NAME test.build.tgt1
  WORKING_DIRECTORY ${CMAKE_BINARY_DIRECTORY}
  COMMAND ${CMAKE_COMMAND} --build . --target tgt1 --config $<CONFIGURATION>
)

This works cross-platform with the same syntax everywhere. It even works with multi-configuration generators, like Visual Studio and Xcode.


Have you tried saying enable_testing() in your CMakeLists.txt file, then calling add_test() to make a test target? I think you can do everything in one consolidated build script if you follow the CMake documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜