TExcelTAD
Home › Modern CMake (with CTest & CPack) › JUnit Reports & CDash

JUnit Reports & CDash

Console output is for people. CI systems want machine-readable results: which tests ran, how long each took, which failed and with what output, so they can show a test tab, track flaky tests and compare runs. CTest offers two formats: JUnit XML, which almost every CI understands, and its own XML for CDash, Kitware's dashboard server.

JUnit XML

--output-junit <file> (CMake 3.21+) writes a JUnit report next to the normal run. GitHub Actions (with a test-reporter action), GitLab CI (artifacts:reports:junit), Jenkins, Azure Pipelines and TeamCity all read it.

bash · try it yourself
mkdir -p rep && cd rep
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Rep LANGUAGES NONE)
enable_testing()
add_test(NAME parser.accepts COMMAND sh -c "echo 'ok: 3 records'")
add_test(NAME parser.rejects COMMAND sh -c "echo 'expected 3 fields, got 2'; exit 1")
add_test(NAME gpu.smoke COMMAND sh -c "exit 77")
set_tests_properties(gpu.smoke PROPERTIES SKIP_RETURN_CODE 77 LABELS gpu)
CM
cmake -S . -B build --log-level=NOTICE > /dev/null
ctest --test-dir build --output-junit junit.xml > /dev/null 2>&1
echo "ctest exit code: $?"
# The XML carries timestamps and host names; show its structure without them.
sed -E '/^\s*(timestamp|hostname|time)=/d; s/ time="[^"]*"//' build/junit.xml
ctest exit code: 8
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="(empty)"
	tests="3"
	failures="1"
	disabled="0"
	skipped="1"
	>
	<testcase name="parser.accepts" classname="parser.accepts" status="run">
		<properties/>
		<system-out>ok: 3 records
</system-out>
	</testcase>
	<testcase name="parser.rejects" classname="parser.rejects" status="fail">
		<failure message="Failed"/>
		<properties/>
		<system-out>expected 3 fields, got 2
</system-out>
	</testcase>
	<testcase name="gpu.smoke" classname="gpu.smoke" status="notrun">
		<skipped message="SKIP_RETURN_CODE=77"/>
		<properties>
			<property name="cmake_labels" value="gpu"/>
		</properties>
		<system-out></system-out>
	</testcase>
</testsuite>

The report has one <testcase> per test, <failure> for failures, <skipped> for skips, and the test's output in <system-out>. A relative --output-junit path is relative to the build directory (build/junit.xml here). In a CI job, upload it as an artifact or point the CI's test report setting at it, and keep the step that runs ctest failing the job when tests fail (exit code 8).

A typical GitHub Actions step pair:

yaml
- name: Test
  run: ctest --test-dir build --output-on-failure --output-junit junit.xml
- name: Upload test results
  if: always()                     # also after failures
  uses: actions/upload-artifact@v4
  with:
    name: junit
    path: build/junit.xml

Dashboard mode: ctest -T

CTest also has a dashboard mode (-T <step> or -D <model><step>) that records each step in XML files under Testing/, ready to submit to CDash. The steps are Start, Update, Configure, Build, Test, Coverage, MemCheck and Submit. Running just the test step works without any server:

bash · try it yourself
mkdir -p dash && cd dash
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Dash LANGUAGES NONE)
include(CTest)                 # enable_testing() + the dashboard options and DartConfiguration.tcl
add_test(NAME one COMMAND true)
add_test(NAME two COMMAND true)
CM
cmake -S . -B build --log-level=NOTICE > /dev/null
cd build
grep -E '^BUILD_TESTING' CMakeCache.txt
ctest -T Test 2>&1 | grep -E 'Test +#|tests passed'
find Testing -name '*.xml' | sed -E 's#[0-9]{8}-[0-9]{4}#<STAMP>#'
BUILD_TESTING:BOOL=ON
1/2 Test #1: one ..............................   Passed    0.00 sec
2/2 Test #2: two ..............................   Passed    0.00 sec
100% tests passed out of 2
Testing/<STAMP>/Test.xml

Note the BUILD_TESTING option: include(CTest) defines it (default ON), and projects wrap their test directories in if(BUILD_TESTING) so packagers can turn tests off. Each dashboard run gets a time-stamped directory with Test.xml; the site (machine name) and build name (OS and compiler) identify the submission on the dashboard.

CDash overview

CDash is an open-source web dashboard (a PHP application; Kitware hosts public instances at my.cdash.org) that collects those XML files from many machines and shows, per project and per day: configure warnings, build errors and warnings, test results with history, coverage and memory-checker defects. Large C++ projects such as VTK, ITK and CMake itself use it for their nightly and continuous builds. The session below is a sample, since it needs a server and network access:

cmake
# CTestConfig.cmake in the source root: where to submit
set(CTEST_PROJECT_NAME "Shapes")
set(CTEST_NIGHTLY_START_TIME "01:00:00 UTC")
set(CTEST_SUBMIT_URL "https://my.cdash.org/submit.php?project=Shapes")
bash
# An "Experimental" submission: configure, build, test and submit in one go
ctest -D Experimental --test-dir build
# Or a scripted dashboard (ctest -S), the usual way for nightly machines:
ctest -S nightly.cmake -V
cmake
# nightly.cmake: a dashboard script
set(CTEST_SITE "ci-linux-01")
set(CTEST_BUILD_NAME "ubuntu24.04-gcc13-release")
set(CTEST_SOURCE_DIRECTORY "$ENV{HOME}/src/shapes")
set(CTEST_BINARY_DIRECTORY "$ENV{HOME}/build/shapes-nightly")
set(CTEST_CMAKE_GENERATOR "Ninja")
ctest_start(Nightly)
ctest_update()
ctest_configure(OPTIONS "-DCMAKE_BUILD_TYPE=Release")
ctest_build()
ctest_test(PARALLEL_LEVEL 8)
ctest_submit()

Dashboard models: Experimental (a developer's ad-hoc run), Nightly (scheduled, tied to the nightly start time) and Continuous (triggered by new commits). Many teams today get the same information from their CI system plus JUnit reports; CDash remains common in scientific and large CMake-based projects, especially for its build-warning tracking across many platforms.

Test yourself

Which ctest option writes a report that GitLab, Jenkins and most CI systems understand?

  1. -T Test
  2. --output-junit <file>
  3. -D Experimental
  4. --output-log

What does include(CTest) add compared to enable_testing()?

  1. Nothing
  2. The BUILD_TESTING option and the dashboard (CDash) configuration, plus enable_testing()
  3. GoogleTest
  4. JUnit output

What is CDash?

  1. A CMake generator
  2. A web dashboard that collects configure, build, test and coverage results submitted by ctest from many machines
  3. A package format
  4. A test framework