Labels, Filters & Test Properties
A project with hundreds of tests needs to run subsets (only the fast ones before a commit, the slow ones nightly), to express tests that should fail, and to stop a hung test. All of that is done with test properties:
set_tests_properties(<test>... PROPERTIES <name> <value> ...)
set_property(TEST <test> APPEND PROPERTY LABELS slow)| Property | Effect |
|---|---|
LABELS |
tags for selecting with ctest -L / -LE |
WILL_FAIL |
invert the result: the test passes when the command fails |
PASS_REGULAR_EXPRESSION |
pass only if the output matches (exit code is then ignored) |
FAIL_REGULAR_EXPRESSION |
fail if the output matches, even with exit code 0 |
SKIP_RETURN_CODE |
this exit code means "skipped" (e.g. no GPU available) |
TIMEOUT |
seconds before the test is killed and counted as failed |
ENVIRONMENT / ENVIRONMENT_MODIFICATION |
environment variables for the test |
WORKING_DIRECTORY |
where the command runs (also an add_test argument) |
DEPENDS |
run after these tests (ordering only, see fixtures for real setup) |
RUN_SERIAL |
never run in parallel with other tests |
COST |
hint for scheduling slow tests first in parallel runs |
All of them in one suite
The "tests" are shell one-liners run through sh -c, which keeps the example short. Real tests would be your test executables.
mkdir -p suite && cd suite
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Suite LANGUAGES NONE)
enable_testing()
add_test(NAME parse.ok COMMAND sh -c "echo 'parsed 3 records'")
add_test(NAME parse.reject COMMAND sh -c "echo 'bad input'; exit 1")
add_test(NAME report.text COMMAND sh -c "echo 'Total: 42'")
add_test(NAME report.warn COMMAND sh -c "echo 'WARNING: rounding'; exit 0")
add_test(NAME gpu.render COMMAND sh -c "echo 'no GPU found'; exit 77")
add_test(NAME env.check COMMAND sh -c "test \"$APP_MODE\" = testing")
add_test(NAME net.download COMMAND sh -c "sleep 5")
set_tests_properties(parse.ok parse.reject PROPERTIES LABELS "fast;parser")
set_tests_properties(parse.reject PROPERTIES WILL_FAIL TRUE)
set_tests_properties(report.text PROPERTIES LABELS fast PASS_REGULAR_EXPRESSION "Total: [0-9]+")
set_tests_properties(report.warn PROPERTIES LABELS fast FAIL_REGULAR_EXPRESSION "WARNING")
set_tests_properties(gpu.render PROPERTIES LABELS gpu SKIP_RETURN_CODE 77)
set_tests_properties(env.check PROPERTIES LABELS fast ENVIRONMENT "APP_MODE=testing")
set_tests_properties(net.download PROPERTIES LABELS slow TIMEOUT 1)
CM
cmake -S . -B build --log-level=NOTICE > /dev/null
cd build
echo "=== list everything"
ctest -N
echo "=== everything"
ctest 2>&1 | grep -E 'Test +#|tests passed|Failed|Timeout'
echo "=== only label 'fast'"
ctest -L fast 2>&1 | grep -E 'Test +#|tests passed'
echo "=== everything except 'slow' and 'gpu', by name regex"
ctest -LE 'slow|gpu' -R '^(parse|env)' | grep -E 'Test +#|tests passed'=== list everything
Test project /home/learner/suite/build
Test #1: parse.ok
Test #2: parse.reject
Test #3: report.text
Test #4: report.warn
Test #5: gpu.render
Test #6: env.check
Test #7: net.download
Total Tests: 7
=== everything
1/7 Test #1: parse.ok ......................... Passed 0.00 sec
2/7 Test #2: parse.reject ..................... Passed 0.00 sec
3/7 Test #3: report.text ...................... Passed 0.00 sec
4/7 Test #4: report.warn ......................***Failed Error regular expression found in output. Regex=[WARNING] 0.00 sec
5/7 Test #5: gpu.render .......................***Skipped 0.00 sec
6/7 Test #6: env.check ........................ Passed 0.00 sec
7/7 Test #7: net.download .....................***Timeout 1.00 sec
71% tests passed, 2 tests failed out of 7
4 - report.warn (Failed) fast
7 - net.download (Timeout) slow
=== only label 'fast'
1/5 Test #1: parse.ok ......................... Passed 0.00 sec
2/5 Test #2: parse.reject ..................... Passed 0.00 sec
3/5 Test #3: report.text ...................... Passed 0.00 sec
4/5 Test #4: report.warn ......................***Failed Error regular expression found in output. Regex=[WARNING] 0.00 sec
5/5 Test #6: env.check ........................ Passed 0.00 sec
80% tests passed, 1 tests failed out of 5
=== everything except 'slow' and 'gpu', by name regex
1/3 Test #1: parse.ok ......................... Passed 0.00 sec
2/3 Test #2: parse.reject ..................... Passed 0.00 sec
3/3 Test #6: env.check ........................ Passed 0.00 sec
100% tests passed out of 3
Read the full run line by line:
parse.rejectpassed although its command exited with 1:WILL_FAILinverts the result.report.textpassed because its output matched the regex.report.warnexited with 0 but failed: its output matchedFAIL_REGULAR_EXPRESSION. Useful for tests that must not log warnings.gpu.renderwas reported as Skipped, not as a failure: exit code 77 is itsSKIP_RETURN_CODE. Skips don't fail the run, but they are listed, so nobody mistakes "skipped" for "passed".env.checksawAPP_MODE=testingfrom itsENVIRONMENTproperty.net.downloadwas killed after 1 second: Timeout, which counts as a failure.
Labels can be combined: -L takes a regex, several -L options must all match (checked with CMake 4.4), and -LE excludes. ctest --print-labels lists every label in use.
Repeating and rerunning
mkdir -p flaky && cd flaky
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Flaky LANGUAGES NONE)
enable_testing()
# Fails on the first run, passes afterwards: a counter file simulates a flaky test.
add_test(NAME flaky COMMAND sh -c "n=$(cat count 2>/dev/null || echo 0); echo $((n+1)) > count; [ $n -ge 1 ]")
add_test(NAME stable COMMAND true)
CM
cmake -S . -B build --log-level=NOTICE > /dev/null
cd build
ctest 2>&1 | grep -E 'Test +#|tests passed'
echo "=== --rerun-failed runs only what failed last time"
ctest --rerun-failed 2>&1 | grep -E 'Test +#|tests passed'
echo "=== --repeat until-pass:3 on a fresh counter"
rm -f count
ctest --repeat until-pass:3 2>&1 | grep -E 'Test +#|tests passed'1/2 Test #1: flaky ............................***Failed 0.00 sec
2/2 Test #2: stable ........................... Passed 0.00 sec
50% tests passed, 1 tests failed out of 2
=== --rerun-failed runs only what failed last time
1/1 Test #1: flaky ............................ Passed 0.00 sec
100% tests passed out of 1
=== --repeat until-pass:3 on a fresh counter
1/2 Test #1: flaky ............................***Failed 0.00 sec
Test #1: flaky ............................ Passed 0.00 sec
2/2 Test #2: stable ........................... Passed 0.00 sec
100% tests passed out of 2
--repeat until-pass:N retries failures (a pragmatic CI setting for known-flaky tests while they get fixed), until-fail:N runs every test N times to find flakiness, and after-timeout:N retries only timeouts. Use the retry modes sparingly: a test that needs retries is a bug report, not a solution.
Test yourself
A test checks that the program rejects bad input (exits with 1). Which property makes it pass in that case?
SKIP_RETURN_CODE 1WILL_FAIL TRUEPASS_REGULAR_EXPRESSIONTIMEOUT 1
How do you run every test except those labelled slow?
ctest -L slowctest -LE slowctest -E slowctest --exclude slow
A test exits with its SKIP_RETURN_CODE. How does ctest report it?
- Passed
- Failed
- Skipped, without failing the run
- Timeout