Question

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 file 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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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