Search code examples
cmakectest

How do I add the -j option to my ctest cmake file?


I would like to run parallel jobs when running ctest. I tried setting

set(CTEST_PARALLEL_LEVEL 8)

in

CTestCustom.cmake.in

but this didn't change the command line options after I re-generated my build files.

I am on windows, using visual studio.


Solution

  • You can't change the command line that is used when you build RUN_TESTS on Visual Studio. There are no options in the code (see cmGlobalGenerator::CreateDefaultGlobalTargets()).

    I see the following possible approaches:

    1. Set the CTEST_PARALLEL_LEVEL environment variable globally in your Windows (so it's just part of you machines CMake/CTest configuration) or add a start script for your Visual Studio.

    2. Create your own target with something like

      add_custom_target(
          RUN_TESTS_J8 
              COMMAND ${CMAKE_CTEST_COMMAND} -j 8 -C $<CONFIGURATION> --force-new-ctest-process 
              WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
      )
      

      And mayby group it together with the other predefined targets

      set_property(GLOBAL PROPERTY USE_FOLDERS ON)
      set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")
      set_property(TARGET RUN_TESTS_J8 PROPERTY FOLDER "CMake")
      
    3. Combining build & run as a POST_BUILD step for the tests itself

      add_custom_command(
          TARGET MyTest
          POST_BUILD
             COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> -R "^MyTest$" --output-on-failures
             WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
      )
      

      Then it would be - as being part of the normal build - by itself executed in parallel.

    More References