CMake variables in CTest?

I am currently playing around with generating CTest scripts and running these using ctest.

In one of those scripts I wanted to create a directory and thought I would be nice and use a platform agnostic approach:

execute_process( COMMAND "${CMAKE_COMMAND}" -E make_directory ${new_directory})

However, I realized quickly that the variable CMAKE_COMMAND is not defined when running ctest. The same is true for the variable CMAKE_CTEST_COMMAND. (Other variables I did not test.)

Is this a known limitation or an oversight?

And if the reason should be that ctest could be run without any cmake executable around, wouldn’t it be useful to give ctest the -E option that cmake has and define at least the CMAKE_CTEST_COMMAND variable?

CMAKE_COMMAND is indeed defined when running ctest. The following CTest dashboard script works for me:



So your approach should work.

Though, incidentally, if you’re trying to create a directory, you can also do the following:

file(MAKE_DIRECTORY ${new_directory})

Ah, ok, I see what you did.
You are calling the script with the ctest option -S. That seems to work.

However, I am just calling it without any option in the directory, where the CTestTestfile.cmake file is located. I do not want to call a dashboard script, I just want to run my built tests.

Try the following and you will notice that the variables are not defined:

  1. Create CTestTestfile.cmake with the following content:
  1. Run ctest in that directory without any arguments.

The output will look like this:

Test project <working-dir>
No tests were found!!!

(Interestingly, messages printed to STDOUT as does message(STATUS ...) are swallowed.)

Thanks, I forgot about that file(MAKE_DIRECTORY ...) command. That helped in my specific case.

Still, I am wondering if the mentioned variables (or even more of the general CMake variables) shouldn’t be defined when running ctest, even without any options.

Indeed you are correct. That’s rather surprising to me… they probably should be defined, especially since TEST_INCLUDE_FILES lets you inject arbitrary code into the CTestTestfile.cmake context.