CTest parallel run by default

CMake environment variable CTEST_PARALLEL_LEVEL controls default test parallellism to save test run wallclock time. CTEST_PARALLEL_LEVEL=0 uses unbounded test parallelism. If the computer runs out of memory or has conflicts with parallel tests, use fixtures and resource locks to control test run parallelism on a per-test basis.

CTest parallel somewhat randomizes the order of the tests. ctest –schedule-random randomizes the order of tests even for serial test runs.

This example run on a 4-core machine shows that no extra command line parameters are needed to use CTEST_PARALLEL_LEVEL:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)

project(par LANGUAGES NONE)

enable_testing()

message(STATUS "ENV{CTEST_PARALLEL_LEVEL}: $ENV{CTEST_PARALLEL_LEVEL}")

foreach(t RANGE 8)
  add_test(NAME sleep${t} COMMAND ${CMAKE_COMMAND} -E sleep 1)
endforeach()
export CTEST_PARALLEL_LEVEL=0

ctest --test-dir build

or

ctest --test-dir build --parallel
Internal ctest changing into directory: C:/temp/build
Test project C:/temp/build
    Start 2: sleep1
    Start 5: sleep4
    Start 3: sleep2
    Start 7: sleep6
1/8 Test #5: sleep4 ...........................   Passed    1.05 sec
    Start 6: sleep5
2/8 Test #7: sleep6 ...........................   Passed    1.04 sec
    Start 1: sleep0
3/8 Test #2: sleep1 ...........................   Passed    1.06 sec
    Start 8: sleep7
4/8 Test #3: sleep2 ...........................   Passed    1.06 sec
    Start 4: sleep3
5/8 Test #1: sleep0 ...........................   Passed    1.06 sec
6/8 Test #6: sleep5 ...........................   Passed    1.06 sec
7/8 Test #8: sleep7 ...........................   Passed    1.05 sec
8/8 Test #4: sleep3 ...........................   Passed    1.05 sec

100% tests passed, 0 tests failed out of 8

Total Test time (real) =   2.14 sec