Run CTest on installed version of project

Can CTest be run on the installed version of a project instead of the version in the CMake build directory?

@mcflugen challenged me with this question and I don’t CTest well enough to answer it. I couldn’t find an answer in the CMake docs or on the internets. My hunch is that this is outside the scope of what CMake/CTest are designed to do, but it would be helpful for us to know if the installed version of a project we’re working on behaves as expected.

What are your test programs that you run with CMake and how do you build and install them?

We do something like

add_executable(d8 d8.c)
target_link_libraries(d8 PRIVATE topotoolbox)
add_test(NAME d8 COMMAND d8)

It seems like both target_link_libraries and add_test can take generator expressions, so you might be able to fiddle around with those to either link to the installed version of a library or call the installed version of an executable. I don’t use generator expressions often enough to know off the top of my head what would work (or even if it definitely would), but that’s what I would look into.

1 Like

My test setup is similar to yours:

add_test(NAME test-irf COMMAND test-irf)
add_executable(test-irf test_irf.c)
target_link_libraries(test-irf
  bmi_cem ${GLIB2_LIBRARIES} ${GTHREAD2_LIBRARIES})

I’ve also experimented with generator expressions. The problem is that the tests are compiled and linked at the cmake --build step.

I imagine there’s some way to wire this up, but it still feels like something CTest isn’t build to do.

I figured out one way to do this using two different buildsystems with a variable that I switch ON when I want to test the installed version:

OPTION(TT_TEST_INSTALLED "Test an installed version of libtopotoolbox" OFF)

if (TT_TEST_INSTALLED)
  find_package(TopoToolbox REQUIRED)
  set(TT_LIB TopoToolbox::topotoolbox)
else()
  set(TT_LIB topotoolbox)
endif()

add_executable(versioninfo versioninfo.cpp)
target_link_libraries(versioninfo PRIVATE ${TT_LIB})
add_test(NAME versioninfo COMMAND versioninfo)

Then assume that I have built and installed v3.0.0 of libtopotoolbox previously:

> cmake -B build/test_installed -DTT_TEST_INSTALLED=ON
> cmake --build build/test_installed
> build/test_installed/test/versioninfo
topotoolbox v3.0.0

Now I make some changes and bump the minor version number to v3.1.0 in my local copy.

> cmake -B build/test_local
> cmake --build build/test_local
> build/test_local/test/versioninfo
topotoolbox v3.1.0

CTest works too. I imagine that you can’t really get around using two different buildsystems because the dependencies are resolved at configure time.

1 Like

@wkearn You are a CMake wizard! Thanks for brewing up this example. I’ll learn from it.