Test CMake - BredaUniversityGames/JenkinsLib GitHub Wiki

cmake.test()

Runs CTest and publishes JUnit results to Jenkins. Supports both preset-based testing (from CMakePresets.json) and manual CTest invocation using the build context.

Usage

With presets:

stages {
    git.sync()
    cmake.build()
    cmake.test()
    // ...
}

Standalone (test preset configures and builds automatically if needed):

stages {
    git.sync()
    cmake.test()
    // ...
}

Parameters

Preset Mode

When CMakePresets.json contains testPresets:

Parameter Default Description
CMAKE_TEST_PRESET (first preset) CTest preset (dropdown populated from CMakePresets.json)

Manual Mode

When no test presets are found, cmake.test() uses the build context from cmake.build(). No additional parameters are added.

Note: In manual mode, cmake.test() requires cmake.build() to run first so it can read the build directory and configuration from context.

How It Works

Preset Mode

  1. Looks up the test preset's configurePreset in CMakePresets.json
  2. If the test preset's configure preset differs from the build preset's configure preset, automatically runs configure + build for the test configuration
  3. Runs ctest --preset <preset> --output-on-failure --output-junit <results>
  4. Publishes JUnit XML results to Jenkins
  5. Marks the build as UNSTABLE if any tests fail

Manual Mode

  1. Reads ctx.cmakeBuildDir and ctx.buildConfig from the build stage context
  2. Runs ctest -C <config> --output-on-failure --output-junit <results> in the build directory
  3. Publishes JUnit XML results to Jenkins
  4. Marks the build as UNSTABLE if any tests fail

Self-Contained Test Builds

When a test preset references a different configurePreset than what was used during cmake.build(), the test stage automatically runs its own configure + build step before testing. This allows test-specific configurations (e.g., with sanitizers or coverage flags) without affecting the main build.

Troubleshooting

Issue Solution
"requires cmake.build() or cmake.workflow() to run first" In manual mode, add cmake.build() before cmake.test()
No test preset dropdown Ensure your CMakePresets.json contains non-hidden testPresets
"has no configurePreset defined" Add a configurePreset field to your test preset in CMakePresets.json
Tests pass locally but fail in CI Check that test fixtures and data files are available in the workspace
⚠️ **GitHub.com Fallback** ⚠️